Dear Friends,
I was keep getting this question from many of you that whenever your outlook signature has an Image then rest of the texts are displayed correctly but Image was never displayed. It was always appearing with a red cross sign (missing image) image. In order to get rid of this problem there are two solutions.
Method 1 : A Trick while displaying the HTML signature (Applicable for any Signature)
Before we jump in to the solution, lets see what goes wrong when we try to display a signature with Image via VBA code. As I had explained in my previous post, How to add signatures from outlook before sending an email via Excel VBA?
Outlook stores all the Signatures in AppData folder in windows in different format like Text, HTML etc. Therefore to display an HTML Signature, first we find the HTML file, read the whole HTML file and append it at the end of the mail body. This is exactly where the problem was when a HTML signature had an Image.
All the texts are part of HTML but as you know for image, HTML has a tag where you provide the path where image is located. All the images are stored in a separate within the Signature folder as you can see in the below picture.
Note: In HTML, if the files are within the same folder where your HTML file is then you do not need to provide the complete path, rather you just give the folder name and file name. HTML by default look for that folder and image within the same directory. This is how outlook reads it and displays the images in mails.
But when we read it from HTML via VBA then this image URL becomes unknown for outlook to display the image in Signature. I hope you understood the problem.
Now you know the problem, then solution is simple :
Solution
Before we use the HTML code for signature at the end of Mail body, we need to change all the path (incomplete URLs) with complete URLs.
Read my comments in the code where I have made the changes.
Method 1: Any Outlook Signature with Image
Sub With_HTML_Signature_With_Image()
'Do not forget to change the email ID
'before running this code
Dim OlApp As Object
Dim NewMail As Object
Dim EmailBody As String
Dim StrSignature As String
Dim sPath As String
Dim signImageFolderName As String
Dim completeFolderPath As String
Set OlApp = CreateObject("Outlook.Application")
Set NewMail = OlApp.CreateItem(0)
' Here Since we are talking about
' the HTML email then we need to
' write the Body of the Email in
' HTML.
EmailBody = "Hello Friends !!" & "
Welcome to LearnExcelMacro.com" & vbNewLine & _
"Here i will make you awesome in Excel Macro.
You can mail me at info@learnexcelmacro.com"
'*****************************************************
' Important
'*****************************************************
' go to the appdata path as mentioned in
' the above important point. Check the name of
' the signature file. For example: here in my system
' the signature's file name is vish.txt, vish.htm
' Therefore before running this code, check the
' name of the signature file in your system and
' replace vish.txt with your file name.
'****************************************************
sPath = Environ("appdata") & "\Microsoft\Signatures\Default.htm"
' Files folder name is always same name as html file name appended by
' "_files"
signImageFolderName = "Default_files"
' in outlook HTML forward slashes are used for
' the file path
completeFolderPath = Environ("appdata") & "\Microsoft\Signatures\" & signImageFolderName
' If the path and file name given by you is not
' correct then code you insert a blank Signature
If Dir(sPath) <> "" Then
StrSignature = GetSignature(sPath)
' Now replace this incomplete file path
' with complete path wherever it is used
StrSignature = VBA.Replace(StrSignature, signImageFolderName, completeFolderPath)
Else
StrSignature = ""
End If
On Error Resume Next
With NewMail
.To = "info@learnexcelmacro.com"
.CC = "info@learnexcelmacro.com"
.BCC = "info@learnexcelmacro.com"
.Subject = "Type your Subject here"
' Here at the end of the Email Body
' HTML Signature is inserted.
.htmlBody = EmailBody & StrSignature
.display
.send
End With
On Error GoTo 0
Set NeMail = Nothing
Set OlApp = Nothing
End Sub
Important : Do not Forget
Do not forget to copy paste the below function in your module
Function to read the Signature file and return the Signature Text
Function GetSignature(fPath As String) As String
Dim fso As Object
Dim TSet As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set TSet = fso.GetFile(fPath).OpenAsTextStream(1, -2)
GetSignature= TSet.readall
TSet.Close
End Function
Method 2 : Simple and Easy Trick (Only for Default Signature)
All you need to do is, in your VBA code, before you assign a Body of your email, display it once on the screen. As soon as VBA displays the mail on screen, outlook, by default, adds its default signature at the end of the body of your email.
Every time before sending the email, if you display your mail, you can see this method will be slow. Therefore it is not good for sending bulk emails in a loop. By the way this method, can be used for all type of signatures (Text, HTML, image etc.). But important to note than this is applicable for Default Signature in outlook only
Important .Display statement should be used anywhere but before the Email Body statement as explained above. If you display your email after putting the email body, then outlook will not append the default signature at the end of your email body.
Sub Outlook_Default_Signature_With_Image()
'Do not forget to change the email ID
'before running this code
Dim OlApp As Object
Dim NewMail As Object
Dim EmailBody As String
Dim StrSignature As String
Dim sPath As String
Set OlApp = CreateObject("Outlook.Application")
Set NewMail = OlApp.CreateItem(0)
' Here Since we are talking about
' the HTML email then we need to
' write the Body of the Email in
' HTML.
EmailBody = "Hello Friends !!" & "
Welcome to LearnExcelMacro.com" & vbNewLine & _
"Here i will make you awesome in Excel Macro.
You can mail me at info@learnexcelmacro.com"
On Error Resume Next
With NewMail
.To = "info@learnexcelmacro.com"
.CC = "info@learnexcelmacro.com"
.BCC = "info@learnexcelmacro.com"
.Subject = "Type your Subject here"
' Important! Before writing the body of your email
' you should display the mail
.display
' Here at the end of the Email Body
' HTML Signature is inserted.
.htmlBody = EmailBody
.send
End With
On Error GoTo 0
Set NeMail = Nothing
Set OlApp = Nothing
End Sub
I am using the code you provided in Office 2016. I use to have the image hidden and attached then placed at the end of the email when I was using office 2010. The update just happened and now the image will not display when it is attached to the email and it also gives me the image can not be displayed issue with your code. Any thoughts on a non default signature with an image? Thank you!
Just wanted to follow up and see if anyone has been able to figure this 2016 issue out?
Dear Bruce,
Sorry for the late response.. are you still facing this issue?
hi, i need you help. i cannot display pictures inside my signature, this is my code:
but i can’t send pictures!!
can you help me?
Hi,
This article was mainly for this issue. If you look at the code which I have in the article, after reading the Signature file, it is important to replace the incomplete image file path with complete file path. Due to this incomplete path of the image, Outlook is not able to display any images in the Signature.
Append the below piece of code in your main code, it should work. Let me know if this helps.
Hi second code is not working:
i have tested it and when you are pasting htmlbody – whole email text is deleted and replaced by hmtlbody text…
This worked a treat for me with MS Office 2013. Thank you very much.
I am glad Jas that it helped you 🙂
it works excelent, thanks from Chile!!
Hello,
i need ur help, because my signature can’t display in my email.
this is my code vba
Excellent, Thanks
Great article. I had an issue with a space in the Signature file name and notices the HTML needed the space to be converted to %20
I am having an issue where the images are now displaying when I send the email to myself but when I send the email to a third party the images are not displaying. Any ideas on what might be causing this?
Olá Vishwamitra
Não entendi os parâmetros If Dir(sPath) <> “” Then
Em lt e gt, qual o caminho a considerar?
Desde já lhe parabenizo pelo ótimo trabalho.
Hello Vishwamitra
I didn’t understand the parameters If Dir(sPath) <> “” Then
In lt and gt, which way to consider?
I congratulate you in advance for the great work.
Hello,
i am using below module for multiple mails but I dont know how to add signature in that.
please help.
Hi, great code, thank you.
However I would like the signature to appear in the open email that I want to send, and not to open a new email as this code does.
Can you tell me what I need to change to do so?
Regards
Harris
Hi
Great code!
However, I would like to add the signature to an existing email instead of opening a new email message.
How do I do this?
Is your code easily adaptable for this?
Hi, I need help on this. I already followed everything. But the image on my signature still could not be displayed.
For anyone struggling to get this working as expected in later versions, I found the only way to get the file to display was to use this line instead:
StrSignature = VBA.Replace(StrSignature, signImageFolderName + “/”, “file:///” + completeFolderPath + “\”)
Also, if your signImageFolderName contains spaces, ensure that the spaces are replaced by %20 to match how it is referenced in the htm file.
Hi Tina,
thank you very much for your hint!
This works:
StrSignature = VBA.Replace(StrSignature, signImageFolderName + "/", "file:///" + completeFolderPath + "\")
and don’t forget in line
signImageFolderName
the %20 for one space.Those who can read have a clear advantage.
You saved my day!
Bye, Georg
In my case it does not work (Office 365): the email contains the html tags, like this:
BODY_TEST
[…]
I hope this modification can run to display image in signature. I have test and it is ok.
Hi,
the second Method above I have tested on my PC. It works ’cause I did come corrections.
This is my version:
Sub Outlook_Default_Signature_With_Image_Method_2()
‘Do not forget to change the email ID before running this code
Dim OlApp As Object
Dim NewMail As Object
Dim EmailBody As String
Dim StrSignature As String
Dim sPath As String
Dim MailBody As Variant
Set OlApp = CreateObject(“Outlook.Application”)
Set NewMail = OlApp.CreateItem(0)
‘ Here Since we are talking about the HTML email then we need to
‘ write the Body of the Email in HTML.
EmailBody = “Hello Friends !!” & _
“Welcome to LearnExcelMacro.com” & _
“Here i will make you awesome in Excel Macro.” & _
“You can mail me at info@learnexcelmacro.com”
On Error Resume Next
With NewMail
.To = “info@learnexcelmacro.com”
.CC = “info@learnexcelmacro.com”
.BCC = “info@learnexcelmacro.com”
.Subject = “Type your Subject here”
‘ Important! Before writing the body of your email
‘ you should display the mail
.Display
‘ Here at the end of the Email Body
‘ HTML Signature is inserted.
MailBody = .HTMLBody
.HTMLBody = EmailBody & MailBody
‘ .send
End With
On Error GoTo 0
Set NewMail = Nothing
Set OlApp = Nothing
End Sub
Tell me if you like it.
Bye, Georg
Dear Vishwamitra
Thank you for your powerfull VBA code 🙂
However, I am experiencing this small problem
– If Dir(sPath) <> “” Then – this part is highlighted in red
I am not sure what went wrong
Please advise
Thank you for your help
Best wishes
Hang Bannon