I tried all the different solutions to this question: How to add default signature in Outlook.
I did not find that any worked with what I have built.
I'm working with an adaptation of Ron de Bruin's email template worksheet where the email body and recipient are referencing another table.
I am either getting the email body correctly formatted (new-line delimited) with broken signature (containing links and images) OR correct signature but the email body is not properly formatted.
The following shows the signature correctly, but the email body is not properly formatted.
On Error Resume Next
Set olApp = Outlook.Application
Set olMail = olApp.CreateItem(olMailItem)
With olMail
.display
End With
signature = olMail.HTMLbody
With olMail
signature = olMail.HTMLbody
.To = StringTo
.CC = StringCC
.BCC = StringBCC
.Subject = Me.Cells(myCell.Row, "I").Value
.HTMLbody = strHTMLBody & Me.Cells(myCell.Row, "K").Value & signature
Give a try to below sub. The sub will display mail with default signature and you can add body message as well as attachment as per your need.
Sub SendMailWithDefaultSign()
On Error GoTo HarunErrHandler
Dim oApp As New Outlook.Application
Dim oEmail As Outlook.MailItem
Dim strAttachment As Variant, strSubject As Variant, strBody As Variant
Dim strEmail As String
Dim fileName As String
strSubject = "VBA code to send mail with default signature." 'Me.TaskID & ": " & Me.TaskTitle
'strBody = Me.Description
strEmail = "recipientmail#domain.com"
Set oEmail = oApp.CreateItem(olMailItem)
With oEmail
.BodyFormat = olFormatHTML
.Display
.Recipients.Add strEmail
.Subject = strSubject
.HTMLBody = "<b>Hello Everyone,</b><br>" & _
"Please cehck the attached file.<br>" & .HTMLBody
' .Attachments.Add fileName
End With
Exit Sub
HarunErrHandler:
MsgBox "Error :" & Err.Number & ", " & Err.Description, vbInformation, "Error"
End Sub
You need to make sure a well-formatted HTML string is assigned to the HTMLBody property. So, if you want to insert anything before the signature you need to find an opening <body> tag and paste your string there right after the <body> tag.
Related
This question already has answers here:
How to add default signature in Outlook
(15 answers)
Closed 1 year ago.
Im having a hard time on adding the default email signature when may code im using right now. it already takes me days already.
*Sub sendemail()
Application.DisplayAlerts = True
Applicatioemphasized textn.EnableEvents = True
Dim OutApp As Object
Dim OutMail As Object
Dim EmailBody As Range
Dim sentto, sentcc, subject As String
sentto = ThisWorkbook.Sheets("DISTRO").Range("B2").Value
sentcc = ThisWorkbook.Sheets("DISTRO").Range("B3").Value
subject = ThisWorkbook.Sheets("DISTRO").Range("B4").Value
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
ThisWorkbook.Sheets("DISTO").Range("B5").Select
Set EmailBody = ThisWorkbook.Sheets("DISTRO").Range("B5").Value
EmailBody.Copy
With OutMail
.To = sentto
.CC = sentcc
.BCC = ""
.subject = subject
.Body = "Hi," & vbNewLine & vbNewLine & "As discussed please see attached file for your PMT score."
.Display
.Attachments.Add ThisWorkbook.Sheets("DISTRO").Range("B8").Value
End With
End Sub*
Give a try to below sub. The sub will display mail with default signature and you can add body message as well as attachment as per your need.
Sub SendMailWithDefaultSign()
On Error GoTo HarunErrHandler
Dim oApp As New Outlook.Application
Dim oEmail As Outlook.MailItem
Dim strAttachment As Variant, strSubject As Variant, strBody As Variant
Dim strEmail As String
Dim fileName As String
strSubject = "VBA code to send mail with default signature."
strEmail = "recipientmail#domain.com"
Set oEmail = oApp.CreateItem(olMailItem)
With oEmail
.BodyFormat = olFormatHTML
.Display
.Recipients.Add strEmail
.Subject = strSubject
.HTMLBody = "<b>Hello Everyone,</b><br>" & _
"Please cehck the attached file.<br>" & .HTMLBody
' .Attachments.Add fileName
End With
Exit Sub
HarunErrHandler:
MsgBox "Error :" & Err.Number & ", " & Err.Description, vbInformation, "Error"
End Sub
Very closely related to Embed picture in outlook mail body excel vba
I'm trying to embed an image into an Outlook email.
I'm using the following code snippet, half of which has been stolen from the post above:
Sub PictureEmail()
Dim outApp As New Outlook.Application
Dim OutMail As Object
Dim Attchmnt As String
Dim Signature As String
Dim WB As Workbook
Set WB = ThisWorkbook
Attchmnt = "C:\Users\Blah\Painted_Lady_Migration.jpg"
Set OutMail = outApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = WB.Names("to").RefersToRange.Value2
.CC = WB.Names("cc").RefersToRange.Value2
.BCC = WB.Names("bcc").RefersToRange.Value2
.Subject = WB.Names("Subject").RefersToRange.Value2
.HTMLBody = "<img src=""cid:Painted_Lady_Migration.jpg""height=520 width=750>"
.display
End With
If Attchmnt = "" Then
Else
OutMail.Attachments.Add Attchmnt
End If
On Error GoTo 0
End Sub
However, when looking at the generated email, I have the error "The linked image cannot be displayed. The file may have been moved, renamed, or deleted".
I've tried a few different ways to attach the file, including:
.HTMLBody = "<img src=" & Chr(34) & "cid:Painted_Lady_Migration.jpg" & Chr(34) & "height=520 width=750>"
I just can't get it to work >_<
I saw somewhere that spaces in the name/filepath can throw it, so I replaced the spaces in the name with underscores
What dumb thing am I forgetting/missing?
The cid is created when you attach it, so you need to do that before you display/send it.
Try it like this
Set OutMail = outApp.CreateItem(0)
With OutMail
.To = WB.Names("to").RefersToRange.Value2
.CC = WB.Names("cc").RefersToRange.Value2
.BCC = WB.Names("bcc").RefersToRange.Value2
.Subject = WB.Names("Subject").RefersToRange.Value2
If Attchmnt <> "" Then
.Attachments.Add Attchmnt ' (additional arguments are optional)
.HTMLBody = "<img src=""cid:Painted_Lady_Migration.jpg"" height=520 width=750>"
Else
.HTMLBody = "[no attachment included]"
End If
.Display
End With
I have an Excel document to log work done during the day to be passed to the nightshift so they are kept up-to-date with the days activites and vice versa.
The plan is to fill out the document and click a 'Send' button that will send the newly created Excel sheet to a shared Outlook folder.
My attempts have been scrapes off the web copied & tried, but to no avail.
Maybe it may help :)
Sub outMail()
Dim outApp As Object
Dim oMail As Object
Dim signature As String
Dim obszar As String
Set outApp = CreateObject("Outlook.Application")
Set oMail = outApp.CreateItem(0)
With oMail
.Display
End With
signature = oMail.Body
With oMail
.To = "email#email.com"
.CC = "email2#email.com"
.BCC = ""
.Subject = "Log work done during the day"
.BodyFormat = 2
.Body = "Hello" & Chr(13) & Chr(10) & "The newly created Excel sheet with log work done during the day " & Chr(13) & Chr(10) & signature
'here You put directory to your file, for now its directory to file where macro is
.Attachments.Add ActiveWorkbook.FullName
'now its set to display only, if You want to send automatically put .send as below
.Display
'.Send
End With
End Sub
If you use HTML for the email body, you will also be able to format the mail body. Just change the file path to attach the mail. If you want to include a new line in the mail body use < br > (without space).
Sub StackOverflow()
Set objOutlook = CreateObject("Outlook.Application")
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
.To = "Johnston#Stackoverflow.com"
.Subject = "Excel Document"
.Display
.HTMLBody = "<p style='font-family:arial;font-size:13'>" & _
"Hi" & "<br>" & "<br>" & _
"Here is the Excel document." & _
.HTMLBody
.Attachments.Add ("C:\Desktop\" & "ExcelDocument.xlsx")
.Display
End With
End Sub
Since I am new to the VBA I am trying to structure a code to send email through outlook with the signature.But when I run the below code it displays me only the signature (body does not appear).
When I use "F8" and check it can be clearly seen that my body appears then signature overwrites it.How do I fix this?
Option Explicit
Sub SendEmailwithsign()
Dim objoutApp As Object, objoutmail As Object
Dim strbody As String, strSig As String
Set objoutApp = CreateObject("outlook.Application")
Set objoutmail = objoutApp.CreateItem(0)
On Error Resume Next
With objoutmail
.To = "AAAAAAAAA#.com"
.CC = ""
.Subject = "Test mail"
.Recipients.ResolveAll
.Display 'body appears until this point'
strSig = .Environ("appdata") & "Microsoft\Signatures\bbbb.txt"
strbody = "Hello"
.body = strbody & strSig 'with this step Body part does not appear but only the signature'
End With
On Error GoTo 0
Set objoutmail = Nothing
Set objoutApp = Nothing
End Sub
You need to change .body to .HTMLBody, please refer to the below code:
Option Explicit
Sub SendEmailwithsign()
Dim objoutApp As Object, objoutmail As Object
Dim strbody As String, strSig As String
Set objoutApp = CreateObject("outlook.Application")
Set objoutmail = objoutApp.CreateItem(0)
On Error Resume Next
With objoutmail
.To = "emailAddress"
.CC = ""
.Subject = "Test mail"
strSig = .Environ("appdata") & "Microsoft\Signatures\default.txt"
strbody = "Hello"
.HTMLBody = "<p>"+ strbody +"</p>" & strSig 'with this step Body part does not appear but only the signature'
.Recipients.ResolveAll
.Display 'body appears until this point'
End With
On Error GoTo 0
Set objoutmail = Nothing
Set objoutApp = Nothing
End Sub
Reference from:
VBA Signature on Email with HTMLBody
I am trying to add signature at the end of the automated mails I am sending. I want the signature to be the default signature of the user that runs the macro. The code I have written runs without crushing but does not insert the signature. I am providing the code below.
Dim OutApp As Object
Dim OutMail As Object
Dim currentDate As Date
Dim DeliveryDate As String
Dim Recipients As String
Dim CarbonCopy As String
Dim Signature As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
currentDate = Format(Date, "dd/mm/yyyy")
Recipients = "a#gmail.com"
CarbonCopy = "b#gmail.com"
Signature = OutMail.body
msg = "<span style='color:black'><p>Dear Team,</p>"
msg = msg & "Thank you in advance</span>"
On Error Resume Next
With OutMail
.To = Recipients
.CC = CarbonCopy
.Subject = "PSR " & currentDate
.HTMLBody = "<span style = 'color:#1F497D'>" & msg & "</span>" & Signature
.Attachments.Add ThisWorkbook.FullName
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
The signature has to be declared as a variant and you have to display the empty email first to capture it.
Your "msg" isn't declared in the above code. I'm assuming you've got that covered. Otherwise your code won't work. Given that assumption...
Dim OutApp As Object
Dim OutMail As Object
Dim currentDate As Date
Dim DeliveryDate As String
Dim Recipients As String
Dim CarbonCopy As String
Dim Signature As Variant
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
currentDate = Format(Date, "dd/mm/yyyy")
Recipients = "a#gmail.com"
CarbonCopy = "b#gmail.com"
Signature = OutMail.Body
'msg hasn't been defined so it's commented out. msg in the body has been replaced with "msg".
'msg = "<span style='color:black'><p>Dear Team,</p>"
'msg = msg & "Thank you in advance</span>"
On Error Resume Next
With OutMail
'Capture signature block.
.Display
Signature = .HTMLBody
.To = Recipients
.CC = CarbonCopy
.Subject = "PSR " & currentDate
.HTMLBody = "<span style = 'color:#1F497D'>" & "msg" & "</span>" & Signature
.Attachments.Add ThisWorkbook.FullName
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
Firstly, you cannot concatenate two HTML strings and expect a valid HTML string back. They must be merged. More than that, you also need to merge the styles etc. from two HTML documents.
Secondly, to retrieve the signature in your case, MailItem must be shown first - call Display, and only then read the HTMLBody property.
If using Redemption is an option (I am its author), it exposes RDOSignature object and allows to insert any signature without displaying the message using RDOSignature.ApplyTo() method.
I think your mailItem does not become active. Try adding OutMail.display before you try getting the body. Then it should work.