Consulting

Results 1 to 8 of 8

Thread: Including HTML Code in Email Body

  1. #1

    Including HTML Code in Email Body

    Hi Guys,

    I have included some prelim code that should send a HTML email that includes my company signature.

    However, it only generates the required signature if I add ".display" method before the .HTML body property within the "With Block".

    If I include .send method after .HTML property it strips the email of formatting and I include the .send method after the .HTML body it generates an error message.

    I am looking to retain the HTML formatting and the .display method - is this possible, please?
    Attached Files Attached Files

  2. #2
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,189
    Location
    You can have .Display just before the .Send as a simple workaround
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved

    Excel 365, Version 2403, Build 17425.20146

  3. #3
    Thank you for your reply, Georgiboy and thank for your help previously with other queries.

    When I add the .display method before the .send method the for loop will trigger and output and send one email but then stop and generate a run-time error: "Item has been moved or deleted"

    Please let me know if there is an alternative workaround

  4. #4
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,189
    Location
    How about if you add the below line of code under the .Display:
    DoEvents
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved

    Excel 365, Version 2403, Build 17425.20146

  5. #5
    Darnit - it is still flagging with the same run time error. Thank you for your help. So frustrating as I am really keen to retain the HTML formatting

  6. #6
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,189
    Location
    Google: ron de bruin send email keep signature vba

    Have a look at the first link that comes up - he is the master at automating Outlook via excel (IMO)
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved

    Excel 365, Version 2403, Build 17425.20146

  7. #7
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,189
    Location
    Here is another example where the signature is captured from the display before it is then sent with the signature:

    Sub test()    
        Dim OApp As Object, OMail As Object, signature As String
        
        Set OApp = CreateObject("Outlook.Application")
        
        For x = 1 To 5
            Set OMail = OApp.CreateItem(0)
            With OMail
                .Display
                DoEvents
            End With
            signature = OMail.HTMLBody
            With OMail
                .to = "jbloggs@somebusiness.com"
                .Subject = "Subject here"
                .HTMLBody = "Body text here" & vbNewLine & signature
                .Send
            End With
        Next x
        
        Set OMail = Nothing
        Set OApp = Nothing
    End Sub
    Last edited by georgiboy; 06-17-2022 at 08:03 AM.
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved

    Excel 365, Version 2403, Build 17425.20146

  8. #8
    No. I think you are the master. The sample code you have provided is amazing and works.

    Thanks so much!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •