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
Change Signatures
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.
Hi Diane, I'm not super familiar with coding, but I wonder if you might be able to advise me on a similar code I found and pasted into a Excel doc I use: Sub savesheet() Dim Name As String Application.ScreenUpdating = False Application.DisplayAlerts = False ActiveWorkbook.Save Name = "Grocery & Dairy 3rd Shift ABSENTEE BLANK (1ST SHIFT)" & ".xlsm" ActiveWorkbook.SaveAs Filename:=Name Application.DisplayAlerts = True Application.ScreenUpdating = True End Sub Sub EmailWBAttached() Dim OutApp As Object Dim OutMail As Object Set OutApp = CreateObject("Outlook.Application") Set OutMail = OutApp.CreateItem(0) On Error Resume Next With OutMail .To = "email@test.com" '<-- enter email addresses here. Multiple emails separate by comma .CC = "" .BCC = "" .Subject = "3rd Shift Attendance: " & Format(Now(), "mm.dd.yy") '<-- enter subject here .Body = "Attached is the attendance sheet or revision to 3rd Shift Grocery & Dairy." '<-- enter message body here .Attachments.Add Application.ActiveWorkbook.FullName… Read more »
Hi Diane,
This is a great script. Best I've seen about adding the signature.
I tried the add signature (in Excel) and works with the exception of three missing images that are present when email is manually created in Outlook. Any suggestions?
Hi there,
can anyone tell me how to enter any text (e.g. Greetings.....plus signature)
into an existing mail that has no body text yet, but it has already recipient
selected.
So the macro should not create an email.
Instead the text should be added into the existing message.
Name of the recipient should be added into the body, like Dear & Recipient...
Thanks a lot in advance.
Branislav
strSignature = .HTMLBody 'the signature is there, so get it before overwriting the body
.HTMLBody = "Hello " & Sheets("OT").Range("L2").Value & "," & strSignature '< concatenate the signature to the end
This basically uses a greeting ("Hello") and looks up the persons name on a spreadsheet and adds the signature afterwards.
Hi Diane,
Can you guide me on how can I use the text before the current email's auto signature bookmark? I am trying to search for a keyword "follow up" in my current reply, to trigger a macro for setting a reminder. It is important to only search the text in the current reply (before the auto signature) to verify whether the keyword exists, and only then trigger the macro else, do nothing.
Thank you in advance.
was looking for something like this and found a VB script instead that creates new signatures and makes it the default. pretty basic but you can insert company logo. is it permitted to post here the code? the original author is still there mentioned in the code.
Yes, you can post it here.
Hi Diane,
Please take a look at my code that uses ribbon and UIAUTOMATION
https://www.developpez.net/forums/blogs/191381-oliv/b4076/inserer-signature-lemail-actif-outlook-2016/
I wouldn't say its any better than reading the signature file and inserting - it's basically sendkeys. (If Outlook supported 'screenupdating = false' it would be better - i wouldn't have to watch the signature selector expand and the mouse move.)
As a demo for UIAUTOMATION, its good - and could be very useful to do other things in Outlook.
The behavior is different if we run the macro from Excel, in this case we can just use DoDefaultAction, but not when we run it from OUTLOOK !
it is useful if the signature contains images.
I tried running this macro in Outlook 2016 and it will not run "Compile error: User-defined type not defined."
Does this only work for certain Outlook versions? Was hoping to get this to work on 2016
It works in all the current versions. In the VBEditor's Tools, References, is the Microsoft Word object library checked? (It looks li8ke i forgot to mention that in the instructions.)
You can edit the code to use late binding (which will avoid the need to set the word object model), but not all word features will be available if you do this - in the macro no this page, specifically the few lines that set a border will error.
To use late binding instead - add these to up at the top with the other dim statements:
Dim Word As Object
Set Word = CreateObject("Word.Application")
then change these two to use object -
Dim olInspector As Outlook.Inspector
Dim olDocument As object 'Word.Document
Dim olSelection As object ' Word.Selection