user13617010
user13617010

Reputation:

Insert a default signature with a picture into an email

I'm trying to write the VBA code that will insert the default Outlook signature into an email that has been generated in Excel. There's a few people that might use the Excel template, so I need the VBA code to pick up the default signature of the user that's creating the email.

I've found various helpful things online, however I can't get it to work. I'm new to VBA so still learning.

The code below works for the email I need - expect the signature as I've removed all that code as I couldn't get it to work.

Any help would be greatly appreciated.

Sub EmailCreator()

ActiveWorkbook.Save

    Dim olApp As Outlook.Application
    Dim olMail As Outlook.MailItem
    Dim blRunning As Boolean

    blRunning = True
    On Error Resume Next
    Set olApp = GetObject(, "Outlook.Application")
    If olApp Is Nothing Then
        Set olApp = New Outlook.Application
        blRunning = False
    End If

    On Error GoTo 0

    strBody = "This is my message in HTML format"

    Set olMail = olApp.CreateItem(olMailItem)

    With olMail
        .Subject = "abc"
        .Attachments.Add ActiveWorkbook.FullName
        .HTMLBody = strBody
        .Display
    End With

    If Not blRunning Then olApp.Quit

    Set olApp = Nothing
    Set olMail = Nothing


End Sub

Upvotes: 0

Views: 237

Answers (1)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66286

Firstly, Outlook will insert the default signature when you call Display if the message body has not been modified yet.

Secondly, once you call Display (and the message body gets populated with the signature), you will need to merge the two HTML strings - not just set the HTMLBody property (which would wipe out the signature), not concatenate the two HTML strings (which cannot produce a valid HTML document), but merge. The easiest would be to look for the position of the "<body" substring, scroll to the next ">" character (to take care of the body tag with attributes), then insert your HTML after that ">" character.

Upvotes: 1

Related Questions