Email from within (1 Viewer)

mjdemaris

Working on it...
Local time
Today, 01:02
Joined
Jul 9, 2015
Messages
426
Hi,
I would like to create a decent looking email using VBA to customize To, From(based on User Name), Subject, Body.

I can get a simple text email to send, but I would like to format it a bit better than using crlf and spaces to align words.

I see that we can set the body type to HTML, but I am not an HTML linguist. Does this mean it is possible to style the paragraph tags in the email?

By the way, I am using an Outlook application mail object.

Thank,
Mike
 

Cronk

Registered User.
Local time
Today, 18:02
Joined
Jul 4, 2013
Messages
2,771
You will need to generate a string containing the message in html.

A simple example
strHTML="<p> Greetings " & rst!FirstName & "</p><p>" _
& "<p> Regarding your email dated " & format(rst!DateReceived, "mm/dd/yyyy") & "..........

I'm not an "HTML linguist" either but a search will give you tags required to be much more tailored in generating emails, such as including images, hyperlinks
 

mjdemaris

Working on it...
Local time
Today, 01:02
Joined
Jul 9, 2015
Messages
426
Thanks, Cronk. I haven't gotten as far as adding tags in the body string, was wondering if they would come out formatted. I'll have to try that today.
 

Cronk

Registered User.
Local time
Today, 18:02
Joined
Jul 4, 2013
Messages
2,771
I should have also added (just in case you were not sure) that the email object's html property should then be set to strHTML
eg objMsg.html= strHTML

For developing and debugging your html generation, I'd suggest you go in small steps with a debug.print strHtml in your code and then saving the result into a text file with the extension .html

Then open that file in your browser to check the rendering.
 

mjdemaris

Working on it...
Local time
Today, 01:02
Joined
Jul 9, 2015
Messages
426
This is what I have so far, without any CSS formatting:
Code:
Public Function HtmlEmail(strReportName As String)
    Dim objFileSys, objTxtStream As Object
    Dim txtHTML As String
    
    DoCmd.OutputTo acOutputReport, strReportName, acFormatHTML, "D:\temp\" & strReportName & ".html", False
    Set objFileSys = CreateObject("Scripting.FileSystemObject")
    Set objTxtStream = objFileSys.opentextfile("D:\temp\" & strReportName & ".html", 1)
    
    txtHTML = objTxtStream.readall
    
    objTxtStream.Close
    
    Set objTxtStream = Nothing
    Set objFileSys = Nothing
    
    Set objOutlookApp = Outlook.Application
    Set objOutlookMail = objOutlookApp.CreateItem(olMailItem)
    
    strToField = "UserName"
    strSubject = "Hello"
    

    
    With objOutlookMail
        .BodyFormat = olFormatHTML
        .HTMLBody = txtHTML
        .Recipients.Add (strToField)
'        objOutLookRecip.Type = olTo
        
        If Len(strCCField) > 1 Then
            .Recipients.Add (strCCField)
            objOutLookRecip.Type = olCC
        End If
        .Subject = strSubject
        
        For Each objOutLookRecip In objOutlookMail.Recipients
            objOutLookRecip.Resolve
        Next
        
        .Send
'        .Display
    End With
    
    Rem Delete Email
    'kill "C:\temp\ & strreportname & ".html"
    
    Set objOutlookApp = Nothing
    Set objOutlookMail = Nothing

End Function
 

Users who are viewing this thread

Top Bottom