The macros in this article will insert a specific signature file into a message.
The first macro creates a new message with a specific signature file inserted. (You can use this method to insert any HTML file into a message.)
Public Sub CreateMessageSignature() Dim objMsg As MailItem Dim strBuffer As String enviro = CStr(Environ("appdata")) Debug.Print enviro Set objFSO = CreateObject("Scripting.FileSystemObject") ' Edit the signature file name on the following line strSigFilePath = enviro & "\Microsoft\Signatures\" Debug.Print strSigFilePath Set objSignatureFile = objFSO.OpenTextFile(strSigFilePath & "My Sig.htm") strBuffer = objSignatureFile.ReadAll objSignatureFile.Close Set objMsg = Application.CreateItem(olMailItem) With objMsg .Subject = "Subject goes here" .HTMLBody = "<p>Something here.</p><p> </p>" & strBuffer .Display End With End Sub
If you need to change a signature in a reply, you will need to remove the signature that was added automatically and insert a new one. If you are creating a new message or not keeping the message body, you can insert an HTML signature directly into .HTMLBody.
This macro removes the default signature and adds a new one. After using this macro, the signature bookmark is removed and you will not be able to change the signature automatically. You will be able to insert another signature manually.
You will need to set a reference to the Microsoft Word Object Library in Tools, References.
Sub ReplywithChangeSig() Dim Item As Outlook.MailItem Dim strBuffer As String enviro = CStr(Environ("appdata")) Set objFSO = CreateObject("Scripting.FileSystemObject") ' Edit the signature file name on the following line strSigFilePath = enviro & "\Microsoft\Signatures\" Debug.Print strSigFilePath Set objSignatureFile = objFSO.OpenTextFile(strSigFilePath & "My Sig.htm") strBuffer = objSignatureFile.ReadAll objSignatureFile.Close Set Item = Application.ActiveExplorer.Selection.Item(1) Dim olInspector As Outlook.Inspector Dim olDocument As Word.Document Dim olSelection As Word.Selection Set myreply = Item.Reply myreply.Display Set olInspector = myreply.GetInspector Set olDocument = olInspector.WordEditor Set olSelection = olDocument.Application.Selection Set oBookmark = olDocument.Bookmarks("_MailAutoSig") If Not oBookmark Is Nothing Then oBookmark.Select olDocument.Windows(1).Selection.Delete End If With olSelection.Borders(wdBorderBottom) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth025pt .Color = wdColorGray10 End With myreply.HTMLBody = "<p> </p>" & strBuffer & myreply.HTMLBody olSelection.MoveStart ' uncomment to send 'myReply.Send End Sub
Sample macros that insert stationary files into new messages are at
How to use the Macro
First: You will need macro security set to low during testing.
To check your macro security in Outlook 2010 and newer, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, it’s at Tools, Macro Security. If Outlook tells you it needs to be restarted, close and reopen Outlook. Note: after you test the macro and see that it works, you can either leave macro security set to low or sign the macro.
Now open the VBA Editor by pressing Alt+F11 on your keyboard.
To put the code in a module:
- Right click on Project1 and choose Insert > Module
- Copy and paste the macro into the new module.
- Set a reference to the Word Object Model in the VBA editor's Tools, References dialog.
More information as well as screenshots are at How to use the VBA Editor.