Image of signature disappearing in email after VBA

Guillaume Waeber 1 Reputation point
2021-03-21T15:54:56.067+00:00

Hi everyone,

I am currently facing an issue programming a macro.

What I am trying to do is generate an email automatically with some of my excel data in the body of the email. Here are my steps:

  • Have some text
  • Add a copied selection from my spreadshhet
  • Add some text again
  • And finally add my signature to the email (I use my standard, preset signature in outlook)

My Signature contains an image with an hyperlink, but the problem is that it disappears in my end result

Here is my code:

Dim Outlook As Object
Dim newEmail As Object
Dim xInspect As Object
Dim pageEditor As Object
Set Outlook = CreateObject("Outlook.Application")
Set newEmail = Outlook.CreateItem(0)

With newEmail
    .Display
End With
    Signature = newEmail.HTMLBody

With newEmail
    .Display
    .To = Sheets("OPOS").Range("H1")
    .CC = ""
    .BCC = ""
    .Subject = "Data"
    .HTMLBody = "Please find the requested information:" & "<br></br>" & "<br></br>" & "<br></br>" & ""

    Set xInspect = newEmail.GetInspector
    Set pageEditor = xInspect.WordEditor
    Sheets("OPOS").Range("A6", "C" & Last_row2 - 2).Copy
    pageEditor.Application.Selection.Start = Len(.Body)
    pageEditor.Application.Selection.End = pageEditor.Application.Selection.Start
    pageEditor.Application.Selection.PasteAndFormat (wdFormatPlainText)
    Set pageEditor = Nothing
    Set xInspect = Nothing

    .HTMLBody = .HTMLBody & "Best regards" & Signature

    .Display

End With

Set newEmail = Nothing
Set Outlook = Nothing

I have noticed that if I add my signature in my first line of .HTMLBody as follows:
.HTMLBody = "Please find the requested information:" & "<br></br>" & "<br></br>" & "<br></br>" & "" & Signature
then the signature appears correctly, with the image.

Does any body knows where the problem lies when I try to add the signature in the end?
For information, I am on Windows 8 and work with MS Office Professional Plus 2016.

Best regards,
Guillaume

{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 118.4K Reputation points
    2021-03-22T11:08:14.867+00:00

    Maybe the Signature is a variable that contains full HTML, i.e. ‘<html>…<body>…</body></html>’. Then the final .HTMLBody, after concatenations, will represent an invalid HTML, which contains unexpected inner <html> element, that cannot be always recognized.

    Make sure that the final .HTMLBody is a valid text according to HTML rules. For example, maybe extract and use the part between <body>…</body> from Signature variable, using some string functions.

    Perhaps you can also use the Value property of Range to get the text from OPOS sheet and use string manipulations instead of inspector and page editor.


  2. Barry 1 Reputation point
    2022-04-22T11:47:07.08+00:00

    Hi,

    I got the same problem and it was resolved using the Collapse method. Try calling it before calling the PasteandFormat

    195604-image.png

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.