0

My emails when received inside of outlook lose the HTML formatting but do not when I open my email in a different application. Any ideas why?

Private Sub Command27_Click()
    DoCmd.OutputTo acOutputReport, "rptInvoice", acFormatPDF, Environ("USERPROFILE") & "\Desktop\MDFInvoice.pdf"
    Dim appOutLook As Object, MailOutLook As Object, insp As Object
    Dim strPath As String, strFilter As String, strFile As String, vbHTML As String

    vbHTML = "<html><head></head><body> Hello! </br></br>"
    vbHTML = vbHTML & "This is an invoice for the MDF Funds please see below, or the attached PDF included for your record keeping purposes </br></br>"
    vbHTML = vbHTML & "Kudelski Security Inc. </br>"
    vbHTML = vbHTML & "Suite 100 </br>"
    vbHTML = vbHTML & "Minnetonka MN 55343 </br>"
    vbHTML = vbHTML & "952-543-699</br>"
    vbHTML = vbHTML & "Tax ID # 41-1961338 </br></br>"
    vbHTML = vbHTML & "<b>Invoice To:</b></br>"
    vbHTML = vbHTML & DLookup("OrgName", "qryRptInvoice") & " </br>"
    vbHTML = vbHTML & DLookup("Street", "qryRptInvoice") & " </br>"
    vbHTML = vbHTML & DLookup("City", "qryRptInvoice") & " " & DLookup("State", "qryRptInvoice") & " " & DLookup("ZipCode", "qryRptInvoice") & " </br></br>"
    vbHTML = vbHTML & "<b>Invoice Details: </b></br>"
    vbHTML = vbHTML & "Date- " & Date & " </br>"
    vbHTML = vbHTML & "Invoice # - " & DLookup("InvoiceNumber", "qryRptInvoice") & " </br>"
    vbHTML = vbHTML & "Terms- " & DLookup("Terms", "qryRptInvoice") & " </br>"
    vbHTML = vbHTML & "Due Date- " & DLookup("DueDate", "qryRptInvoice") & " </br>"
    vbHTML = vbHTML & "Subsidiary- " & DLookup("Subsidiary", "qryRptInvoice") & " </br>"
    vbHTML = vbHTML & "Currency- " & DLookup("Currency", "qryRptInvoice") & " </br>"
    vbHTML = vbHTML & "Total Amount- " & Format(DSum("LineAmount", "qryRptInvoice"), "Currency") & "</body></html>"

    strPath = Environ("USERPROFILE") & "\Desktop\report.pdf"      'Edit to your path
    If strPath <> "" Then
        Set appOutLook = CreateObject("Outlook.Application")
        Set MailOutLook = appOutLook.CreateItem(olMailItem)

        With MailOutLook
            .To = Me.txtTestEmail
            .Subject = "MDF Invoice Kudelski Security"
            .HTMLBody = vbHTML
            .BodyFormat = olFormatHTML
            .Attachments.Add strPath
            .Send
        End With
    Else
        MsgBox "No file matching " & strPath & strFilter & " found." & vbCrLf & _
                "Processing terminated."
        Exit Sub
    End If
End Sub
braX
  • 11,506
  • 5
  • 20
  • 33
Doug Coats
  • 6,255
  • 9
  • 27
  • 49
  • We always struggle with outlook when implementing messages in any application. You could check this out if it helps: https://stackoverflow.com/q/11137540/5373542 – Fabrizio Valencia Jun 30 '19 at 18:57
  • 2
    Try putting `.BodyFormat = olFormatHTML` before `.HTMLBody = vbHTML`. It's always best to define format before loading content to avoid casting troubles. – Erik A Jun 30 '19 at 22:18
  • Thanks @ErikA but that doesnt work either. I have been reading the links Fabrizio sent but so far I havent come to a successful conclusion. – Doug Coats Jul 01 '19 at 13:39
  • Also why was this down voted? – Doug Coats Jul 01 '19 at 13:39
  • Also, Id argue whoever submitted this for being closed is just plain wrong. This particular issue is specific to VBA coding and the HTML application with different mail clients. It is both valid and apparently more of a common place issue than I would have originally thought. So, if the person who decided it was worth flagging would explain themselves I'd love to hear their "reasoning" behind it. – Doug Coats Jul 01 '19 at 13:43
  • For the no MCVE (not my vote, downvote also not mine) I imagine you could add the HTML being generated. It might be as simple as invalid HTML which makes Outlook process it wrong. You're not entity-encoding any of the values you look up, so if there are special characters in there this script will cause unexpected results. – Erik A Jul 01 '19 at 13:44
  • @ErikA I actually tested it in a .html file before moving it over to VBA. Although my web dev skill slack severely I thought that wouldve been sufficient? I guess Ill start using one of the tools mentioned in another thread. I guess I have some wrestling to do with outlook :( – Doug Coats Jul 01 '19 at 13:47
  • You can always run it through an HTML validator. There are significant differences between how Outlook processes HTML, and how a web browser like Chrome does it. Generally, Outlook is a lot less forgiving and has a lot more weird layout issues. – Erik A Jul 01 '19 at 13:49
  • Yeah def learning that now lol – Doug Coats Jul 01 '19 at 13:55
  • Aaaaaaan turns out the issue was my
    tags. The back slash is in the wrong place. Yay
    – Doug Coats Jul 01 '19 at 14:35

3 Answers3

0

First of all, make sure you end up with a well-formed HTML markup. Issues with setting the message bodies in case of non-well-formed markup may lead to treating your content as a plain text.

Second, there is no need to set up the BodyFormat property explicitly because you deal with the HTMLBody property.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
0

I would get rid of the

<html><head></head><body>

at the start and end of your string construct. Outlook uses it's own weird wrapper for the content, so that might be confusing things.

Minty
  • 1,616
  • 1
  • 8
  • 13
0

I guess I should post the answer to this as an answer instead of a comment, which I did sometime ago -

Aaaaaaan turns out the issue was my
tags. The back slash is in the wrong place. Yay

Doug Coats
  • 6,255
  • 9
  • 27
  • 49