Emailing Exported Excel Files From Within Access

It is not sensitive. That is why I use names and not actual email addresses.
If it had been, I would have changed the code before posting.

You would need to change the recordset.

FWIW here is a display, the contents just changes depending on deposit or payment, but you should get the idea.

HTH

I was a little surprised at the simple output from all that coding but it did help me understand it. There was a function missing from the code that dealt with the GetBoiler but I was able to find it online and add it to make the signature code work.

On that subject... my signature displays all but one detail. There is a linked image in the sig that won't display. I have attached screenshots to show what I mean.

The signature in the generated email:

attachment.php


What the signature should look like:

attachment.php


Below is the code for my email... do you know what needs to be added to the signature part to make that linked image show?
Code:
Option Compare Database
Option Explicit

Public Function EmailNotice()

    Dim appOutLook As Outlook.Application
    Dim MailOutLook As Outlook.MailItem
    Dim rst As DAO.Recordset, rst2 As DAO.Recordset, rst3 As DAO.Recordset, rst4 As DAO.Recordset
    Dim strAppdata As String, strSigPath As String, strSignature As String
    Dim strEMailTo As String, strEMailCC As String, strBody As String
    Dim intBody As Integer
    Dim varUSMParts As Variant, varAIMParts As Variant
    
    ' Get appdata path
    strAppdata = Environ("Appdata")
    
    ' Set paths
    strSigPath = strAppdata & "\Microsoft\Signatures\Primary.htm"

    'Get the signature if it exists
    If Dir(strSigPath) <> "" Then
    
        strSignature = GetBoiler(strSigPath)
        
    End If

        Set appOutLook = CreateObject("Outlook.Application")
        Set MailOutLook = appOutLook.CreateItem(olMailItem)
        Set rst = CurrentDb.OpenRecordset("select EmailAddress from tbl_users where AccessLvl in (2, 3, 4) ")
  
    rst.MoveFirst
        
    Do While Not rst.EOF
        strEMailTo = strEMailTo & "; " & rst!EmailAddress
        rst.MoveNext
  
    Loop
  
        Set rst2 = CurrentDb.OpenRecordset("select EmailAddress from tbl_receivers where Active = True ")
  
        rst2.MoveFirst
        
    Do While Not rst2.EOF
        strEMailCC = strEMailCC & "; " & rst2!EmailAddress
        rst2.MoveNext
    
    Loop
    
        Set rst3 = CurrentDb.OpenRecordset("select PartNumber from tbl_parts where USMonthly = True ")
        
    rst3.MoveFirst
    
    Do While Not rst3.EOF
        varUSMParts = varUSMParts & rst3!PartNumber & ", "
        rst3.MoveNext
  
    Loop
    
        Set rst4 = CurrentDb.OpenRecordset("select PartNumber from tbl_parts where AIMonthly = True ")
        
    rst4.MoveFirst
    
    Do While Not rst4.EOF
        varAIMParts = varAIMParts & rst4!PartNumber & ", "
        rst4.MoveNext
  
    Loop

    strBody = "<font face=Calibri>Attention all," & "<br><br>" & _
              "This email is to alert you that it is time to perform the monthly electrical parts audit." & "<br><br>" & _
              "Receivers, please pull the newest P.O. of electrical parts to be audited and contact the auditor with part numbers and their P.O. quantities." & "<br><br>" & _
              "USA Receivers, the part numbers that need to be included are listed below. If all are not on one P.O. then pull as many P.O.'s as necessary to complete the list." & "<br>" & _
              varUSMParts & "<br><br>" & _
              "Aisia Receivers, the part numbers that need to be included are listed below. If all are not on one P.O. then pull as many P.O.'s as necessary to complete the list." & "<br>" & _
              varAIMParts & "<br><br>" & _
              "Auditors, you will need to coordinate with your receivers to have these parts delivered to you for auditing." & "<br><br>" & _
              "Thank you everyone for all of your efforts!</font>" & "<br><br><br>" & strSignature
  
    With MailOutLook
    
    .To = strEMailTo
    .BodyFormat = olFormatRichText
    .CC = strEMailCC
    .Subject = "Monthly Electrical Audit Alert - " & Date
    .HTMLBody = strBody
'    .Send
    .Display      'Used during testing without sending (Comment out .Send if using this line)

    End With
    
End Function

Function GetBoiler(ByVal sFile As String) As String
Dim fso As Object
Dim ts As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(sFile).OpenAsTextStream(1, -2)
GetBoiler = ts.readall
ts.Close
End Function
maybe it has to do with the GetBoiler function using sFile as string or OpenAsTextStream but I do not know at all.
 

Attachments

  • Capture.JPG
    Capture.JPG
    52.9 KB · Views: 349
  • Capture1.JPG
    Capture1.JPG
    54.8 KB · Views: 334
Last edited:
Sorry.
When I created most of that code it was under 2003 and that did not have the option of SendUsingAccount, so I did it via a template.

In work I used something similar, just not with a template as that version is 2007 and I wanted to send under my default account anyway.
My 2003 template did not have any image in the signature anyway, but my works one did.?

So in the works version I just use
Code:
   strDiv = "<div class=WordSection1>"
    strBody = "<p>" & strGreeting & "</p>" & pstrMessage
    ' Now comes from email table
    'strBody = strBody & "<p>" & strTimeLimit & "</p><p>"
    strBody = strBody & "<p>Should there be any queries please let me know.</p>"

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    
    ' Need to get correct account to send on
    For Each OutAccount In OutApp.Session.Accounts
        If OutAccount.DisplayName = pstrCompany Then
            Exit For
        End If
    Next

    On Error Resume Next
   ' Change the mail address and subject in the macro before you run it.
    With OutMail
        .Display
        .To = pStrToNames
        .BCC = ""
        .Subject = pstrSubject
        .htmlbody = Replace(.htmlbody, strDiv, strDiv & strBody)
        '.attachments.Add pstrFilename & ".pdf
        .Attachments.Add pstrFilename

        .Send
    End With

Now I inspected the email which is in HTml format to see where I could insert my body of the message.
In doing that I did not affect the rest of the email signature image included and so that was sent as normal.

You are replacing the whole HTML body with your code, so that will wipe out any reference to any images I would have thought? Also you are specifying body format of RTF and then using Html body?

I can only show what works for me, and when I got it working, I heaved a sigh of relief and left it at that. :D

So try that method, no template required in that case.

HTH
 
You are replacing the whole HTML body with your code, so that will wipe out any reference to any images I would have thought? Also you are specifying body format of RTF and then using Html body?

I also noticed it was set to olFormatRichText and tried changing that to olFormatHTML which didn't change anything.

Interestingly enough... if I select a new email from Outlook the signature is added correctly but when access starts a new email it doesn't. Seems like there is a lot of code related to adding a signature that should automatically be added by outlook in the first place just by something calling a new email no different than clicking the "New Email" button within outlook.

I have read so many threads all over the interweb related to this but no solution I can find.

I did read a post by Isladogs from 2016 on another site where he stated that GetBoiler wont work with an image in the sig
 
So try my works version?

I was looking at that but I do not understand it. Seems there is a macro that has something to do with the addresses and subject matter?

I think there is a some missing information to explain the pStrToNames and pStrSubject etc. (among others) I am too novice to understand it.
 
They are just variables I pass in to make it it more functional.
They should be self explanatory by their names?

I just had a thought/memory, try it without the .Display and see if the signature is present, and then try it with the .Display.

I just have had some recollection that I needed to do that to get the image in the signature, otherwise I had no need for it in this scenario in work.

You still need to insert your content into the html code of the message though, not overwrite it, or construct your own entirely.
 
They are just variables I pass in to make it it more functional.
They should be self explanatory by their names?

I just had a thought/memory, try it without the .Display and see if the signature is present, and then try it with the .Display.

I just have had some recollection that I needed to do that to get the image in the signature, otherwise I had no need for it in this scenario in work.

You still need to insert your content into the html code of the message though, not overwrite it, or construct your own entirely.

I read it real good and it still didn't make sense so I decided to copy it in as-is and start from there and after declaring all of your variables so the code would run... VIOLA! the signature displayed correctly!

After that I was able to mix back in my code to display what I wanted in the body and all is good.

gasman... thank you so much for your help. No way I could have done it without you.
 
FYI... here is the final code:
Code:
Option Compare Database
Option Explicit

Public Function EmailNotice()

    Dim Outapp As Object, OutMail As Object, OutAccount As Object
    Dim rst As DAO.Recordset, rst2 As DAO.Recordset, rst3 As DAO.Recordset, rst4 As DAO.Recordset
    Dim varUSMParts As Variant, varAIMParts As Variant
    Dim strDiv As String, strGreeting As String, strBody As String
    Dim strEMailTo As String, strEMailCC As String
    Dim pstrMessage As String, pstrCompany As String
    
    Set rst3 = CurrentDb.OpenRecordset("select PartNumber from tbl_parts where USMonthly = True ")
        rst3.MoveFirst
    Do While Not rst3.EOF
        varUSMParts = varUSMParts & rst3!PartNumber & ", "
        rst3.MoveNext
    Loop

    Set rst4 = CurrentDb.OpenRecordset("select PartNumber from tbl_parts where AIMonthly = True ")
        rst4.MoveFirst
    Do While Not rst4.EOF
        varAIMParts = varAIMParts & rst4!PartNumber & ", "
        rst4.MoveNext
    Loop

    strDiv = "<div class=WordSection1>"

    strBody = strBody & "<font face=Calibri>Attention all," & "<br><br>" & _
              "This email is to alert you that it is time to perform the monthly electrical parts audit." & "<br><br>" & _
              "Receivers, please pull the newest P.O. of electrical parts to be audited and contact the auditor with part numbers and their P.O. quantities." & "<br><br>" & _
              "USA Receivers, the part numbers that need to be included are listed below. If all are not on one P.O. then pull as many P.O.'s as necessary to complete the list." & "<br>" & _
              varUSMParts & "<br><br>" & _
              "Wanek Receivers, the part numbers that need to be included are listed below. If all are not on one P.O. then pull as many P.O.'s as necessary to complete the list." & "<br>" & _
              varAIMParts & "<br><br>" & _
              "Auditors, you will need to coordinate with your receivers to have these parts delivered to you for auditing." & "<br><br>" & _
              "Thank you everyone for all of your efforts!</font>" & "<br>"

    Set Outapp = CreateObject("Outlook.Application")
    Set OutMail = Outapp.CreateItem(0)
    
    Set rst = CurrentDb.OpenRecordset("select EmailAddress from tbl_users where AccessLvl in (2, 3, 4) ")
        rst.MoveFirst
    Do While Not rst.EOF
        strEMailTo = strEMailTo & "; " & rst!EmailAddress
        rst.MoveNext
    Loop
    
    Set rst2 = CurrentDb.OpenRecordset("select EmailAddress from tbl_receivers where Active = True ")
        rst2.MoveFirst
    Do While Not rst2.EOF
        strEMailCC = strEMailCC & "; " & rst2!EmailAddress
        rst2.MoveNext
    Loop

    With OutMail
        .Display
        .To = strEMailTo
        .CC = strEMailCC
        .BCC = ""
        .Subject = "Monthly Electrical Audit Alert - " & Date
        .HTMLBody = Replace(.HTMLBody, strDiv, strDiv & strBody)
        '.attachments.Add pstrFilename & ".pdf
        '.Attachments.Add pstrFilename

        '.Send
    End With
    
    End Function
 
Please try it with the .Display commented out and see if you still get the signature, and report back. Just have this nagging thought in my head.

TIA

FYI... here is the final code:
Code:
Option Compare Database
Option Explicit

Public Function EmailNotice()

    Dim Outapp As Object, OutMail As Object, OutAccount As Object
    Dim rst As DAO.Recordset, rst2 As DAO.Recordset, rst3 As DAO.Recordset, rst4 As DAO.Recordset
    Dim varUSMParts As Variant, varAIMParts As Variant
    Dim strDiv As String, strGreeting As String, strBody As String
    Dim strEMailTo As String, strEMailCC As String
    Dim pstrMessage As String, pstrCompany As String
    
    Set rst3 = CurrentDb.OpenRecordset("select PartNumber from tbl_parts where USMonthly = True ")
        rst3.MoveFirst
    Do While Not rst3.EOF
        varUSMParts = varUSMParts & rst3!PartNumber & ", "
        rst3.MoveNext
    Loop

    Set rst4 = CurrentDb.OpenRecordset("select PartNumber from tbl_parts where AIMonthly = True ")
        rst4.MoveFirst
    Do While Not rst4.EOF
        varAIMParts = varAIMParts & rst4!PartNumber & ", "
        rst4.MoveNext
    Loop

    strDiv = "<div class=WordSection1>"

    strBody = strBody & "<font face=Calibri>Attention all," & "<br><br>" & _
              "This email is to alert you that it is time to perform the monthly electrical parts audit." & "<br><br>" & _
              "Receivers, please pull the newest P.O. of electrical parts to be audited and contact the auditor with part numbers and their P.O. quantities." & "<br><br>" & _
              "USA Receivers, the part numbers that need to be included are listed below. If all are not on one P.O. then pull as many P.O.'s as necessary to complete the list." & "<br>" & _
              varUSMParts & "<br><br>" & _
              "Wanek Receivers, the part numbers that need to be included are listed below. If all are not on one P.O. then pull as many P.O.'s as necessary to complete the list." & "<br>" & _
              varAIMParts & "<br><br>" & _
              "Auditors, you will need to coordinate with your receivers to have these parts delivered to you for auditing." & "<br><br>" & _
              "Thank you everyone for all of your efforts!</font>" & "<br>"

    Set Outapp = CreateObject("Outlook.Application")
    Set OutMail = Outapp.CreateItem(0)
    
    Set rst = CurrentDb.OpenRecordset("select EmailAddress from tbl_users where AccessLvl in (2, 3, 4) ")
        rst.MoveFirst
    Do While Not rst.EOF
        strEMailTo = strEMailTo & "; " & rst!EmailAddress
        rst.MoveNext
    Loop
    
    Set rst2 = CurrentDb.OpenRecordset("select EmailAddress from tbl_receivers where Active = True ")
        rst2.MoveFirst
    Do While Not rst2.EOF
        strEMailCC = strEMailCC & "; " & rst2!EmailAddress
        rst2.MoveNext
    Loop

    With OutMail
        .Display
        .To = strEMailTo
        .CC = strEMailCC
        .BCC = ""
        .Subject = "Monthly Electrical Audit Alert - " & Date
        .HTMLBody = Replace(.HTMLBody, strDiv, strDiv & strBody)
        '.attachments.Add pstrFilename & ".pdf
        '.Attachments.Add pstrFilename

        '.Send
    End With
    
    End Function
 
Please try it with the .Display commented out and see if you still get the signature, and report back. Just have this nagging thought in my head.

TIA

Well... not only did it not show the signature... but no body as well

When I use .Display and it shows it to me and I click the send button in Outlook everything shows fine.

Sup wit dat?
 
Well I would not expect it to show any body, as you have not set it at that point, but it confirms (I think) my bad memory, in that the .Display is required for the image at least.

In my SSAFA version (2003) I do not display the email unless I specifically ask for it, yet I have the signature as it comes with the template?

In the works version, there is no real need for that statement, as I just send, so I must have put it there for some reason.:D

Main thing is we got to the bottom of it, and it could help others.
 
This might be something to do with it.

If you send a Word document or Excel workbook directly from within the app, I have found that there is no signature shown, and you have to insert it manually.
 
This might be something to do with it.

If you send a Word document or Excel workbook directly from within the app, I have found that there is no signature shown, and you have to insert it manually.

I was thinking along the same lines last night... since we aren't actually opening an email before sending it.... Outlook doesn't insert the signature. I remember seeing some posts somewhere about someone mentioning they would see a brief mail open and close like a flicker. I don't remember where I saw that or what code was being used but maybe that is more like what needs to happen if using the .Send option rather than .Display.

.Display is opening an email in Outlook and .Send is not.
 
Well if anyone is interested...

The solution to that issue was to have the .Display AND the .Send active so it briefly opens an email giving Outlook time to include the signature and then sends and closes it. Just a flicker is all you see.
 

Users who are viewing this thread

Back
Top Bottom