add signature in vba outlook

ummjay

Board Regular
Joined
Oct 1, 2010
Messages
193
Hi, I have the below running, but for some reason it doesn't include the user's signature? Any ideas? Note, different users may be using this, so would like to include whatever their default outlook signature is:

VBA Code:
'Send Email
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)


'On Error Resume Next
With OutMail
strbody = .htmlbody
    
    .To = "test"""
    .CC = "test@test.com"
    .Subject = "Recap for " & Name
    .htmlbody = RangetoHTML(rng)
    '.htmlbody = getNewHTML(.htmlbody, strName, 2, "XXNameXX")
    '.htmlbody = strBody & "<br>" & .htmlbody
    .Display

End With
On Error GoTo 0

Set OutMail = Nothing
          
cleanup:
    Set OutApp = Nothing
    Application.DisplayAlerts = False
    Application.DisplayAlerts = True

    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
End Sub

Thanks!
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Try
Rich (BB code):
.htmlbody = RangetoHTML(rng) & .htmlbody
The added portin is in bold
 
Upvote 0
That will preserve the outlook signature, if the user had set one in their profile
 
Upvote 0
Try also moving the '.Display' part to the top of that section:
VBA Code:
Sub test()
    Dim rng As Range
    'Send Email
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
   
    Set rng = Range("C4:D5")
   
    'On Error Resume Next
    With OutMail
        .Display
        .To = "test"""
        .CC = "test@test.com"
        .Subject = "Recap for " & Name
        .htmlbody = RangetoHTML(rng) & .htmlbody
    End With
    On Error GoTo 0
   
    Set OutMail = Nothing
         
cleanup:
    Set OutApp = Nothing
    Application.DisplayAlerts = False
    Application.DisplayAlerts = True

    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
End Sub
 
Upvote 0
Hi! So I added the .display first, like you suggested. I can see it composed my email with my signature, then when it went to add the table/htmlbody, it pasted that, and removed the signature. So seems that's whats causing it to be removed.

here is my rangetohtml code

VBA Code:
Function RangetoHTML(rng As Range)
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook

    TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"

    rng.Copy
    Set TempWB = Workbooks.Add(1)
    With TempWB.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With

    With TempWB.PublishObjects.Add( _
         SourceType:=xlSourceRange, _
         Filename:=TempFile, _
         Sheet:=TempWB.Sheets(1).Name, _
         Source:=TempWB.Sheets(1).UsedRange.Address, _
         HtmlType:=xlHtmlStatic)
        .Publish (True)
    End With

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    RangetoHTML = ts.ReadAll
    ts.Close
    RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                          "align=left x:publishsource=")

    TempWB.Close savechanges:=False

    Kill TempFile

    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing

End Function
 
Upvote 0
Thus try replacing the line .htmlbody = RangetoHTML(rng) & .htmlbody with:
VBA Code:
myTxt = RangetoHTML(rng)
.HTMLBody = myTxt & .HTMLBody
 
Upvote 0
Solution
Did you use the below?
VBA Code:
.htmlbody = RangetoHTML(rng) & .htmlbody

Or just?
VBA Code:
.htmlbody = RangetoHTML(rng)
 
Upvote 0

Forum statistics

Threads
1,223,879
Messages
6,175,145
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top