Add your signature to pre-formatted emails


August 25, 2008 – 3:46 pm by JP

    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.

signaturefornew-253x300

    Here is your sample code:

VBA:
  1. Sub StockMsgWithSignatureVersion1()
  2.  
  3. Dim NewMsg As Outlook.MailItem
  4.  
  5. Set NewMsg = CreateItem(olMailItem)
  6.  
  7. NewMsg.Display
  8.  
  9. With NewMsg
  10.   .Subject = "Here's my subject"
  11.   .HTMLBody = "<p>Hello,</p>" & _
  12.     "<p>Here's my daily stats report: </p>" & _
  13.   "<p>Number of emails I ignored: <br />" & _
  14.   "Meetings missed: <br />" & _
  15.   "Files deleted by mistake: <br />" & _
  16.   "Presentations not upated: </p>" & .HTMLBody
  17. End With
  18.  
  19. Set NewMsg = Nothing
  20. 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 <br /> and <p> 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:

VBA:
  1. Sub StockMsgWithSignatureVersion2(strSubject As String, Ignored As Long, _
  2. Missed As Long, Files As Long, Pres As Long)
  3.  
  4. Dim NewMsg As Outlook.MailItem
  5.  
  6. Set NewMsg = CreateItem(olMailItem)
  7.  
  8. NewMsg.Display
  9.  
  10. With NewMsg
  11.   .Subject = strSubject
  12.   .HTMLBody = "<p>Hello,</p>" & _
  13.     "<p>Here's my daily stats report: </p>" & _
  14.   "<p>Number of emails I ignored: " & Ignored & "<br />" & _
  15.   "Meetings missed: " & Missed & "<br />" & _
  16.   "Files deleted by mistake: " & Files & "<br />" & _
  17.   "Presentations not upated: " & Pres & "</p>" & .HTMLBody
  18. End With
  19.  
  20. Set NewMsg = Nothing
  21. End Sub

VBA:
  1. Sub CallMySub()
  2.   Call StockMsgWithSignatureVersion2("Hey now!", 30, 3, 5, 8)
  3. 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:

VBA:
  1. Sub StockMsgWithSignatureVersion3()
  2.  
  3. Dim NewMsg As Outlook.MailItem
  4. Dim objInsp As Outlook.Inspector
  5. Dim cbc As Office.CommandBarPopup
  6. Dim cbControls As Office.CommandBarControls
  7. Dim cbButton As Office.CommandBarButton
  8.  
  9. Set NewMsg = CreateItem(olMailItem)
  10.  
  11. NewMsg.Display
  12.  
  13. Set objInsp = ActiveInspector
  14. If Not objInsp Is Nothing Then
  15.   Set cbc = objInsp.CommandBars.FindControl(, 31145)
  16. End If
  17.  
  18. If Not cbc Is Nothing Then
  19.   Set cbControls = cbc.Controls
  20. End If
  21.  
  22. For Each cbButton In cbControls
  23.     If cbButton.Caption = "Signature" Then
  24.         cbButton.Execute
  25.         Exit For
  26.     End If
  27. Next
  28.  
  29. With NewMsg
  30.   .Subject = "Here's my subject"
  31.   .HTMLBody = "<p>Hello,</p>" & _
  32.     "<p>Here's my daily stats report: </p>" & _
  33.   "<p>Number of emails I ignored: <br />" & _
  34.   "Meetings missed: <br />" & _
  35.   "Files deleted by mistake: <br />" & _
  36.   "Presentations not upated: </p>" & .HTMLBody
  37. End With
  38.  
  39. ExitProc:
  40. Set cbControls = Nothing
  41. Set cbc = Nothing
  42. Set objInsp = Nothing
  43. Set NewMsg = Nothing
  44. 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


Share and Enjoy:
  • StumbleUpon
  • Technorati
  • Digg
  • Google
  • del.icio.us
  • MisterWong

Print This Post Print This Post  |  Email This Post Email This Post  |  Permalink  |  Subscribe to this feed Subscribe now!

Filed Under: Outlook, VBA
Tags: ,

This post has 367 views since August 25, 2008 – 3:46 pm.

Post a Comment

To post VBA code in your comment, use [VBA] tags, like this: [VBA]Code goes here[/VBA].





Subscribe without commenting

Keep Reading:

Browse Posts:


« Processing multiple emails || Forget about it! »