Add your signature to pre-formatted emails

Ever wanted to have an army of pre-formatted emails, which you can send at the click of a button, but couldn't figure out how to programmatically insert your signature at the end? Here's one method that should work for you.

First you should make a note of whether your signature is appended automatically to new emails. If you're not sure, start Outlook and go to Tools > Options > Mail Format and check the 'Signature for new messages' box. If you have a named signature there, then your chosen signature is being added to every new message you originate.

signature

Here is your sample code:

Sub StockMsgWithSignatureVersion1()

Dim NewMsg As Outlook.MailItem

Set NewMsg = CreateItem(olMailItem)

NewMsg.Display

With NewMsg
  .Subject = "Here's my subject"
  .HTMLBody = "<p>Hello,</p>" & _
    "<p>Here's my daily stats report: </p>" & _
  "<p>Number of emails I ignored: <br />" & _
  "Meetings missed: <br />" & _
  "Files deleted by mistake: <br />" & _
  "Presentations not upated: </p>" & .HTMLBody
End With

Set NewMsg = Nothing
End Sub

Very simply, we create a new mail message and update the body with our chosen text, moving the signature below the added text. Keep in mind this is the HTMLBody property, so we don't use vbCr or vbCrLf when we want a new line; we have to use HTML tags like
and

to format the email body.

We can get more complicated with this. We can add arguments to the sub, which are then called from another sub which can be assigned to a toolbar button. For example:

Sub StockMsgWithSignatureVersion2(strSubject As String, Ignored As Long, _
Missed As Long, Files As Long, Pres As Long)

Dim NewMsg As Outlook.MailItem

Set NewMsg = CreateItem(olMailItem)

NewMsg.Display

With NewMsg
  .Subject = strSubject
  .HTMLBody = "<p>Hello,</p>" & _
    "<p>Here's my daily stats report: </p>" & _
  "<p>Number of emails I ignored: " & Ignored & "<br />" & _
  "Meetings missed: " & Missed & "<br />" & _
  "Files deleted by mistake: " & Files & "<br />" & _
  "Presentations not upated: " & Pres & "</p>" & .HTMLBody
End With

Set NewMsg = Nothing
End Sub
Sub CallMySub()
  Call StockMsgWithSignatureVersion2("Hey now!", 30, 3, 5, 8)
End Sub

The sub CallMySub would be added to a toolbar button (see Resend This Message for instructions). Just create several copies of it, with different parameters, and give each toolbar button a different name.

But what about if your signature is not being inserted automatically? Here is the code you should use:

Sub StockMsgWithSignatureVersion3()

Dim NewMsg As Outlook.MailItem
Dim objInsp As Outlook.Inspector
Dim cbc As Office.CommandBarPopup
Dim cbControls As Office.CommandBarControls
Dim cbButton As Office.CommandBarButton

Set NewMsg = CreateItem(olMailItem)

NewMsg.Display

Set objInsp = ActiveInspector
If Not objInsp Is Nothing Then
  Set cbc = objInsp.CommandBars.FindControl(, 31145)
End If

If Not cbc Is Nothing Then
  Set cbControls = cbc.Controls
End If

For Each cbButton In cbControls
    If cbButton.Caption = "Signature" Then
        cbButton.Execute
        Exit For
    End If
Next

With NewMsg
  .Subject = "Here's my subject"
  .HTMLBody = "<p>Hello,</p>" & _
    "<p>Here's my daily stats report: </p>" & _
  "<p>Number of emails I ignored: <br />" & _
  "Meetings missed: <br />" & _
  "Files deleted by mistake: <br />" & _
  "Presentations not upated: </p>" & .HTMLBody
End With

ExitProc:
Set cbControls = Nothing
Set cbc = Nothing
Set objInsp = Nothing
Set NewMsg = Nothing
End Sub

The above code sets an object reference to the Insert|Signature command on the toolbar (using the FindControl ID) and loops through all the buttons on the submenu until it finds one with the name "Signature" (which is the name of the signature we want to use). Otherwise it's exactly the same as the code at the top of the post. Make sure to substitute the name of the signature you want to use.

Enjoy,
JP

About JP

I'm just an average guy who writes VBA code for a living. This is my personal blog. Excel and Outlook are my thing, with a sprinkle of Access and Word here and there. Follow this space to learn more about VBA. Keep Reading »


Related Articles:


Share This Article:

Share and bookmark this articledelicious buttonfacebook buttonlinkedin buttonstumbleupon buttontwitter button

comment bubble 1 Comment(s) on Add your signature to pre-formatted emails:

  1. toter writes:

    What about Insert|Signature with RTF body?

    .Body = "Bla bla bla" & _
    .Body

    does not work correct…

This article is closed to any future comments.
Peltier Tech Charting Utilities for Excel