Email using user selected HTML Template with optional report as attachment

russ0670

New member
Local time
Today, 13:12
Joined
Jun 27, 2015
Messages
8
Hi am am really struggling with this piece of coding.
I have an access form. It has



-Two textboxes with client first and last name
-Two textboxes with a contact 'email1' and 'email2' for the client
-A combobox with a list of templates to use for the email.
-A checkbox to include a copy of the statement on the email.


I want the user to be able to press a button which does the following


-Sends an email to both 'email1' and 'email2'.
-Attaches a secific report as PDF if the user has selected the checkbox
-The body of the email includes a greeting line using the clients name from the record.
-The body of the message uncludes below the greeting line a html email template depending on the users combobox selection.


I have searched so many forums and i can find pleanty of code to send an email but i cannot get any of it to work for the above example.Any help would be greatly appreciated.
 
SOLVED Kind of.

Used the following code and works great.

However does anyone know if there is a way to add dynamic text above the content of a .oft template in the email. e.g Set a greeting line based on the form data and then put this greeting line above the generic content from the template??

I have tried adding a .body however this then overrides the .oft template and only this displays on the email.

Code:
Private Sub Emailer2()

DoCmd.SetWarnings False
On Error GoTo cmdEmail_Err

Dim oItem As Outlook.MailItem
Dim oOutlookApp As Outlook.Application
Dim strAtt
DoCmd.Hourglass True

If oOutlookApp Is Nothing Then

Set oOutlookApp = CreateObject("Outlook.application")

Else

Set objOutlook = GetObject(, "Outlook.Application")

End If


Set oItem = oOutlookApp.CreateItemFromTemplate("C:\FastFile\System 3.0\3.0\BLANKTEMP.oft")
    strAtt = "C:\FastFile\System 3.0\3.0\sampleatt.pdf"
    With oItem
        .Subject = "SAMPLE EMAIL SUBJECT"
        .Attachments.Add strAtt, olByValue, 24
        .Display
        .To = "test2@testing.com"

    End With
Set oItem = Nothing
DoCmd.Hourglass False
DoCmd.SetWarnings True
Exit Sub
cmdEmail_Err:
    DoCmd.Hourglass (False)
    MsgBox "You cancelled and did not send the email", vbExclamation
    DoCmd.SetWarnings True
    
End Sub
 
I would expect the newly created item to be a copy of the template and any changes made to it shouldn't affect the template.

Can you show the full code you used to change the Body of the template?
 
I did mine like so.

Split the HTML code and insert your text.

Code:
' Automate the routine to send notifications of Payments and deposits for clients
Dim strLastCell As String
Dim strDate As String
Dim strType As String, strClient As String, str3rdID As String, str3rdParty As String, str3rdPartyType As String, strAmount As String, strRef As String, strMethod As String
Dim strCaseWorker As String, strDatetype As String, strPad As String, strEndPad As String, strPadCol As String, strNotes As String
Dim iLastRow As Integer, iColon As Integer
Dim blnDisplayMsg As Boolean
Dim rngCell As Range

' Now the Outlook variables
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment
Dim strSigPath As String, strSignature As String, strAttachFile As String
Dim strHeader As String, strFooter As String, strBody As String, strTemplatePath As String, strAppdata As String
Dim intBody As Integer

' Set up HTML tags
strPad = "<tr><td>"
strEndPad = "</td></tr><tr></tr>"
strPadCol = "</td><td>  </td><td>"

On Error GoTo Err_Handler

'Establish all the static Outlook Data

    ' Get appdata path
    strAppdata = Environ("Appdata")
    
    ' Set paths
    strTemplatePath = strAppdata & "\Microsoft\Templates"
    strSigPath = strAppdata & "\Microsoft\Signatures\Ssafa.htm"
    
    
    'Get the signature if it exists
    If Dir(strSigPath) <> "" Then
        strSignature = GetBoiler(strSigPath)
        intBody = InStr(strSignature, "<BODY>")
        strHeader = Left(strSignature, intBody + 5)
        strFooter = Mid(strSignature, intBody + 6)
    End If

    ' Create the Outlook session.
    Set objOutlook = GetObject(, "Outlook.Application")
    'Set objOutlook = New Outlook.Application
    

' I get my data here
        
        ' Now populate the outlook fields
        ' Create the message.
        'Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
        
        Set objOutlookMsg = objOutlook.CreateItemFromTemplate(strTemplatePath & "\SSAFA Email.oft")
    
        With objOutlookMsg
            ' Add the To recipient(s) to the message.
            Set objOutlookRecip = .Recipients.Add("* - SSAFA Swansea")
            objOutlookRecip.Type = olTo
    
            ' Add the CC recipient(s) to the message.
            Set objOutlookRecip = .Recipients.Add("** SSAFA West Glamorgan Branch")
            objOutlookRecip.Type = olCC
            
            If strCaseWorker <> "" Then
                Set objOutlookRecip = .Recipients.Add(strCaseWorker)
                objOutlookRecip.Type = olCC
            End If
    
            ' Set the Format, Subject, Body, and Importance of the message.
            .BodyFormat = olFormatHTML
            strDatetype = "Date "
            If strType = "Payment" Then
                .Subject = " Payment Made - " & strClient
                str3rdPartyType = "Recipient: "
                strDatetype = strDatetype & "Paid: "
            Else
                .Subject = "Deposit Received - " & strClient
                str3rdPartyType = "Received From: "
                strDatetype = strDatetype & "Received: "
    
            End If
            
            ' Now build the body of the message
            
            ' Make sure we have a colon in client, else use whole field
            iColon = InStr(strClient, ":")
            If iColon = 0 Then iColon = Len(strClient) + 1
            
            .HTMLBody = strHeader & "<table><tr>"
            .HTMLBody = .HTMLBody & "<td>" & "Client: " & strPadCol & Left(strClient, iColon - 1) & strEndPad
            .HTMLBody = .HTMLBody & strPad & str3rdPartyType & strPadCol & str3rdParty & strEndPad
            .HTMLBody = .HTMLBody & strPad & strDatetype & strPadCol & strDate & strEndPad
            .HTMLBody = .HTMLBody & strPad & "Payment Method: " & strPadCol & strMethod & strEndPad
            .HTMLBody = .HTMLBody & strPad & "Reference: " & strPadCol & strRef & strEndPad
            .HTMLBody = .HTMLBody & strPad & "Payment Amount: " & strPadCol & strAmount & strEndPad
            ' Add any notes if they exist
            If Len(strNotes) > 0 Then
                .HTMLBody = .HTMLBody & strPad & "Notes: " & strPadCol & strNotes & strEndPad
            End If
            .HTMLBody = .HTMLBody & "</table>" & strFooter
            '.Importance = olImportanceHigh  'High importance
            'Debug.Print strHeader
            'Debug.Print .htmlbody
            'Debug.Print strFooter
            ' Resolve each Recipient's name.
            For Each objOutlookRecip In .Recipients
                objOutlookRecip.Resolve
            Next
    
            ' Should we display the message before sending?
            If blnDisplayMsg Then
                .Display
            Else
                .Save
                .Send
            End If
        End With
'    End If

' Now get the next record
    
   ' ActiveCell.Offset(1, 0).Select
   Debug.Print rngCell
' Loop

Next rngCell

' Switch off the filter
ActiveSheet.AutoFilterMode = False
ActiveWorkbook.Save

Proc_Exit:
    Exit Sub
    
Err_Handler:
    MsgBox Err.Number & " " & Err.Description
    Resume Proc_Exit



End Sub
 

Users who are viewing this thread

Back
Top Bottom