Adding default signature in draft email

51 Views Asked by At

I'm trying to generate a draft email using VBA code from excel file. I'm stuck from adding the default signature and maintains its format. My current code can add the signature but the format has been changed. What should I need to change to capture the exact format of signature, I tried the .Display and used the .HTMLBody but the the signature doesn't shown up but some hyperlink which I do not understand where coming from.

Here's my current code:

Sub DraftEmail()
Dim objOutlook As Outlook.Application
Dim objMail As Outlook.MailItem
Dim lCounter As Long
Dim signature As String

'Set objects
Set objOutlook = Outlook.Application
        
'Show confirmation message to user
    MsgBox "Draft Email Generated.", vbInformation
           
'Create a new draft email
    Set objMail = objOutlook.CreateItem(olMailItem)
    objMail.Display
    signature = objMail.Body

'Read details from Draft Email Sheet
For lCounter = 6 To 6
    
    'To
    objMail.To = Worksheets("Draft Email").Range("A" & lCounter).Value
    
    'Cc
    objMail.CC = Worksheets("Draft Email").Range("B" & lCounter).Value
    
    'Subject
    objMail.Subject = Worksheets("Draft Email").Range("C" & lCounter).Value
    
    'Draft Email Body
    objMail.Body = Worksheets("Draft Email").Range("D" & lCounter).Value & objMail.Body
    
    'Add Attachment
    'objMail.Attachments.Add (Draft Email.Range("E" & lCounter).Value)
    
    'Close the object
    Set objMail = Nothing
Next
End Sub

Thanks in advance! :)

2

There are 2 best solutions below

0
niton On

Appears Dim signature As String is the source of the confusion.

Option Explicit

Sub DraftEmail_appendSignatureAsVariant()

Dim objOutlook As Outlook.Application
Dim objMail As Outlook.MailItem

Dim signature As Variant

Dim objOutlook As New Outlook.Application
Set objMail = objOutlook.CreateItem(olMailItem)

objMail.Display
signature = objMail.HTMLBody

objMail.HTMLBody = "Value" & signature

End Sub
0
niton On

You can append the signature without a signature variable.

Sub DraftEmail_AppendHTMLBodyDirectly()

Dim objOutlook As New Outlook.Application
Dim objMail As Outlook.MailItem

Set objOutlook = Outlook.Application
Set objMail = objOutlook.CreateItem(olMailItem)

objMail.Display

objMail.HTMLBody = "Value" & objMail.HTMLBody

End Sub