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.
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
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.
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 have been trying all morning to get this VBA script to attach my active excel document to an auto-generated outlook message. Everything works fine if I declare the file path as a string and attach it. Except that I would like to attach the full file path of the current excel document instead of using a static string value.
Here is my code:
Private Sub CommandButton1_Click()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim sAttach As String
Dim sTo As String
Dim sCC As String
'For To field
Set emailRng = Worksheets("Pre-Clearance Email").Range("E11:J14")
For Each cl In emailRng
sTo = sTo & ";" & cl.Value
Next
sTo = Mid(sTo, 2)
'For CC field
Set emailRngCC = Worksheets("Pre-Clearance Email").Range("E16:J19")
For Each cl In emailRngCC
sCC = sCC & ";" & cl.Value
Next
sCC = Mid(sCC, 2)
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
'variable declarations for email body and attachment
strbody = "<BODY style=font-size:11pt;font-family:Calibri>Good Morning;<p>Please see the attached aliases for validation. Please let me know if you have any questions.<p>Thank you.</BODY>"
sAttach = "K:\CRM Support\Data\Systematic Trade Recon (1).xlsm"
'the below code adds a users default signature to the email
With OutMail
.Display
End With
signature = OutMail.HTMLBody
With OutMail
.to = sTo
.CC = sCC
.Subject = "STR Pre-Clearance"
.HTMLBody = strbody & signature
.Attachments.Add (ActiveDocument.FullName)
'.Attachments.Add sAttach
.Display 'Instead of .Display, you can use .Send to send the email _
or .Save to save a copy in the drafts folder
End With
The compiler gives me an error at this line:
.Attachments.Add (ActiveDocument.FullName)
I have done some research, and tried to fix the problem myself, but I just can't figure out how to make this script attach the active file to this outlook message. As you can see by my code, my backup option is to just use a string variable and a static address to attach the file, but I would rather make this script more versatile than that.
Here is one of the sites which I found that gave me this idea to begin with: Here
Well, after some more effort I was able to get the workbook to attach flawlessly. Here was the revision I made to the OutMail Object in my orginial code:
With OutMail
.to = sTo
.CC = sCC
.Subject = "STR Pre-Clearance"
.HTMLBody = strbody & signature
.Attachments.Add (ActiveDocument.FullName) 'this is the correction I made
.Display
I figured I would answer my own question so it doesn't linger without a technical answer. Maybe it will help someone in the future.
The fix should actually be:
With OutMail
.To = sTo
.CC = CC
.Subject = "STR Pre-Clearance"
.HTMLBody = strbody & signature
.Attachments.Add (ActiveWorkbook.FullName) 'should be workbook not document
.Display 'or .Send
I am trying to set up several buttons on an Excel form to email different groups of people.
I made several ranges of cells on a separate worksheet to list the email addresses.
For example, I want "Button A" to open Outlook and put the list of email addresses from "Worksheet B: Cells D3-D6". Then all that has to be done is hit "Send" in Outlook.
Sub Mail_workbook_Outlook_1()
'Working in 2000-2010
'This example send the last saved version of the Activeworkbook
Dim OutApp As Object
Dim OutMail As Object
EmailTo = Worksheets("Selections").Range("D3:D6")
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = EmailTo
.CC = "person1#email.com;person2#email.com"
.BCC = ""
.Subject = "RMA #" & Worksheets("RMA").Range("E1")
.Body = "Attached to this email is RMA #" & Worksheets("RMA").Range("E1") & ". Please follow the instructions for your department included in this form."
.Attachments.Add ActiveWorkbook.FullName
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
.Display
End With
On Error Goto 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
You have to loop through every cell in the range "D3:D6" and construct your To string. Simply assigning it to a variant will not solve the purpose. EmailTo becomes an array if you assign the range directly to it. You can do this as well but then you will have to loop through the array to create your To string
CODE
Option Explicit
Sub Mail_workbook_Outlook_1()
'Working in 2000-2010
'This example send the last saved version of the Activeworkbook
Dim OutApp As Object
Dim OutMail As Object
Dim emailRng As Range, cl As Range
Dim sTo As String
Set emailRng = Worksheets("Selections").Range("D3:D6")
For Each cl In emailRng
sTo = sTo & ";" & cl.Value
Next
sTo = Mid(sTo, 2)
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = sTo
.CC = "person1#email.com;person2#email.com"
.BCC = ""
.Subject = "RMA #" & Worksheets("RMA").Range("E1")
.Body = "Attached to this email is RMA #" & _
Worksheets("RMA").Range("E1") & _
". Please follow the instructions for your department included in this form."
.Attachments.Add ActiveWorkbook.FullName
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
ToAddress = "test#test.com"
ToAddress1 = "test1#test.com"
ToAddress2 = "test#test.com"
MessageSubject = "It works!."
Set ol = CreateObject("Outlook.Application")
Set newMail = ol.CreateItem(olMailItem)
newMail.Subject = MessageSubject
newMail.RecipIents.Add(ToAddress)
newMail.RecipIents.Add(ToAddress1)
newMail.RecipIents.Add(ToAddress2)
newMail.Send
Both answers are correct.
If you user .TO -method then the semicolumn is OK - but not for the addrecipients-method. There you need to split, e.g. :
Dim Splitter() As String
Splitter = Split(AddrMail, ";")
For Each Dest In Splitter
.Recipients.Add (Trim(Dest))
Next