Insert Picture Signature Without Email Display
I have been searching the internet for a VBA code to insert my picture signature to my email and found many posts stating that to insert a picture signature you will have to display the email which when used, had a dramatic impact on the speed of the program. It increased the time by more than 20x as measured on my laptop.
After many trials and more errors, I figured out that to Insert Picture Signature without having to Display the email you must change the relative paths of the signature html code to absolute path. This is what the below function does.
Private Function getHtmlSignature (SignatureName As String) As String
'Getting the outlook default path of the signature html file
Dim sFile As String
sFile = Environ("appdata") & "\Microsoft\Signatures\" & SignatureName & ".htm"
'Reading the signature HTML file in a string variable
Dim sSignature As String
sSignature = .OpenTextFile(sFile, 1).readall
'Getting the relative path as written in the signature html file
Dim htmlRelPath As String
htmlRelPath = SignatureName & "_files/"
'Getting the full path of the signature files and replacing any space with %20
Dim htmlFullPath As String
htmlFullPath = VBA.Replace("file:///" & Environ("appdata") & "/Microsoft/Signatures/" & SignatureName & "_files/", " ", "%20")
'replacing the backward slash \ with a forward Slash / in the path string
htmlFullPath = VBA.Replace(htmlFullPath, "\", "/")
'replacing the relative path with full path in the signature variable
getHtmlSignature = VBA.Replace(sSignature, htmlRelPath, htmlFullPath)
Below is how you create the email and call the above function to insert your signature
'Getting the Signature HTML code in a string variable using the above function
Dim Sig as string
Sig = getHtmlSignature(SignatureName)
'Creating Outlook object
Dim olApp As Object
Set olApp = CreateObject("Outlook.Application")
'Creating the email
Dim oMail As Object
Set oMail = olApp.createitem(0)
'adding subject and body
.Subject = "Subject goes here"
.To = "firstname.lastname@example.org"
.htmlBody = "Your email body text goes here"
'Now this is the code to add the signature.
.htmlBody = .htmlBody & vbCrLf & Sig
'Sending the email