How can I add the Outlook Signature to my VBA program?

erisciveja

New Member
Joined
Apr 3, 2023
Messages
2
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Sub sendCustEmails()


Dim objOutlook As Object
Set objOutlook = CreateObject("Outlook.Application")
Dim objEmail As Object
Set objEmail = objOutlook.CreateItem(olMailItem)
Dim Signature As String


intRow = 2

While (ThisWorkbook.Sheets("Client_Data").Range("A" & intRow).Text <> "")

Set objEmail = objOutlook.CreateItem(olMailItem)
strMailSubject = ThisWorkbook.Sheets("Mail_Details").Range("A2").Text
strMailBody = ThisWorkbook.Sheets("Mail_Details").Range("B2").Text

intRow = 2
strAudit = ThisWorkbook.Sheets("Client_Data").Range("A" & intRow).Text
strPrefix = ThisWorkbook.Sheets("Client_Data").Range("B" & intRow).Text
strName = ThisWorkbook.Sheets("Client_Data").Range("C" & intRow).Text
strEmail = ThisWorkbook.Sheets("Client_Data").Range("D" & intRow).Text
strAttachment = ThisWorkbook.Sheets("Client_Data").Range("E" & intRow).Text
strSignature = ThisWorkbook.Sheets("Client_Data").Range("F" & intRow).Text

strMailBody = Replace(strMailBody, "<Prefix>", strPrefix)
strMailBody = Replace(strMailBody, "<Name>", strName)
strMailBody = Replace(strMailBody, "<Audit>", strAudit)
strMailBody = Replace(strMailBody, "<Signature>", strSignature)


With objEmail
.To = CStr(strEmail)
.Subject = strMailSubject
.Body = strMailBody & Signature
.Attachments.Add strFolder & "\" & strAttachment
.Display
End With

intRow = intRow + 1

Wend

MsgBox "Done"

End Sub


This code currently displays an Outlook Email before I send it out. Everything seems fine, the attachment is also there. But the only issue is that I can't display the Signature. Can anybody review this code and help me out please?
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Welcome to the MrExcel forum. Please accept my warmest greetings and sincere hope that all is well.

Replace these lines:
VBA Code:
  With objEmail
    .To = CStr(strEmail)
    .Subject = strMailSubject
    .Body = strMailBody & Signature
    .Attachments.Add strFolder & "\" & strAttachment
    .Display
  End With

For these:
VBA Code:
    With objEmail
      .To = CStr(strEmail)
      .Subject = strMailSubject
      .Attachments.Add strFolder & "\" & strAttachment
      .Display
      .HtmlBody = strMailBody & .HtmlBody
    End With

--------------
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
--------------
 
Upvote 0
Hello Dante. Thank you very much for your help. The signature does show up and it works now. However, the email is displayed all together without any spacing making it very hard to read. Is there a way to add spacing?
 
Upvote 0
However, the email is displayed all together without any spacing making it very hard to read. Is there a way to add spacing?

You will understand that I have no idea how your email body is in your cell of the "Mail_Details" sheet. I can only guess how that information is.
Assuming you have something like this:

1680553773621.png


Update cell B2 with something like this, notice how I'm putting <br> where I want a newline:
1680553872858.png

I made some improvements to the code that I can't pass up, so try this code:
VBA Code:
Sub sendCustEmails()
  Dim objOutlook As Object, objEmail As Object
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim intRow As Long
  Dim strAudit$, strPrefix$, strName$, strEmail$, strAttachment$, strSignature$
  Dim strMailSubject$, strMailBody$, strFolder$
  
  Set objOutlook = CreateObject("Outlook.Application")
  Set sh1 = ThisWorkbook.Sheets("Mail_Details")
  Set sh2 = ThisWorkbook.Sheets("Client_Data")
  
  strMailSubject = sh1.Range("A2").Text
  strMailBody = sh1.Range("B2").Text
  
  intRow = 2
  While sh2.Range("A" & intRow).Text <> ""
    Set objEmail = objOutlook.CreateItem(0)
    strAudit = sh2.Range("A" & intRow).Text
    strPrefix = sh2.Range("B" & intRow).Text
    strName = sh2.Range("C" & intRow).Text
    strEmail = sh2.Range("D" & intRow).Text
    strAttachment = sh2.Range("E" & intRow).Text
    strSignature = sh2.Range("F" & intRow).Text
    
    strMailBody = Replace(strMailBody, "<Prefix>", strPrefix)
    strMailBody = Replace(strMailBody, "<Name>", strName)
    strMailBody = Replace(strMailBody, "<Audit>", strAudit)
    strMailBody = Replace(strMailBody, "<Signature>", strSignature)
    
    With objEmail
      .To = CStr(strEmail)
      .Subject = strMailSubject
      .Attachments.Add strFolder & "\" & strAttachment
      .Display
      .HtmlBody = strMailBody & .HtmlBody
    End With
    
    intRow = intRow + 1
  Wend
  MsgBox "Done"
End Sub

--------------
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
--------------
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,956
Members
449,096
Latest member
Anshu121

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