Insert Signature in Outlook Email
Solution 1
Here is an example of code I used to attach signature to mail.
Sub Email()
'Working in 2000-2010
'This example send the last saved version of the Activeworkbook
Dim OutApp As Object
Dim OutMail As Object
Dim Path As String: Path = Sheet10.Range("H6").Text & " " & Sheet10.Range("I6").Text
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "Attached updated Stock."
On Error Resume Next
With OutMail
.Display
.To = "[email protected]"
.CC = ""
.BCC = ""
.Subject = "Fruits Stock " & Path
.HTMLBody = strbody & .HTMLBody
.Attachments.Add ActiveWorkbook.FullName
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Solution 2
Outlook is an HTML application. You will need to use the .HTMLBody and at the end of any text you may put in make sure to include .HTMLBody = "text"& .HTMLBody
. This .HTMLBody
has to be at the end of any body portion of your email for the signature to appear automatically. I would avoid trying to use multiple body types and just stick with HTML when coding for Outlook. When coding for Outlook use the general format provided in the link by @shmicah in the comments. If your signature does not appear where you want it then add
for each line you need to go down.
Solution 3
Signatures are added by MailItem.Display
only if the message body was not modified before Display
is called. Call Display
first, and only then merge existing message body (which will include the signature) with your own data. And if you want to preserve the formatting, you will need to work with the HTMLBody
property, not the plain text Body
. But that will mean you must merge two HTML strings appropriately - you cannot just concatenate them.
If using Redemption (I am its author) is an option, it exposes the RDOSignature object. You can use its ApplyTo
method to insert any signature into any message - it takes of the correctly merging the HTML, including styles and embedded mage attachments.
set Session = CreateObject("Redemption.RDOSession")
Session.MAPIOBJECT = Application.Session.MAPIOBJECT
set Drafts = Session.GetDefaultFolder(olFolderDrafts)
set Msg = Drafts.Items.Add
Msg.To = "[email protected]"
Msg.Subject = "testing signatures"
Msg.HTMLBody = "<html><body>some <b>bold</b> message text<br></body></html>"
set Account = Session.Accounts.GetOrder(2).Item(1) 'first mail account
if Not (Account Is Nothing) Then
set Signature = Account.NewMessageSignature
if Not (Signature Is Nothing) Then
Signature.ApplyTo Msg, false 'apply at the bottom
End If
End If
Msg.Display
The Gootch
I'm not much of a coder, but I love making my life easier at work, especially when it comes to creating macros that do the redundant work for me. I also enjoy learning new things about code, so I try asking questions and getting out of them as much as I can.
Updated on June 04, 2022Comments
-
The Gootch almost 2 years
I found this code, but it doesn't include the signature. In all the resources I've read, I couldn't come up with a solution.
Public Function Email_Test() Dim MyOutlook As Outlook.Application Dim MyMail As Outlook.MailItem Dim objOutlook As Object Dim Attach As String Set MyOutlook = New Outlook.Application Set MyMail = MyOutlook.CreateItem(olMailItem) MyMail.To = "[email protected]" '**put in reference to form 'MyMail.CC = MailList("Copy To") MyMail.Subject = "This Is A Test Email" '**put in reference for subject MyMail.Body = "Hi," & vbNewLine & vbNewLine & "See attached." 'MyMail.Send MyMail.Display Set MyMail = Nothing Set MyOutlook = Nothing End Function
-
shmicah about 6 years
-