More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

share code 23-May-2019 09:50

Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? You′ve come to the right place!

NEW! >> Subscribe to this site via RSS. For more RSS options, see the complete list of feeds on our main news page.

Code level: advanced    Code area: Outlook Expert Techniques Printer Friendly Version
Title: Insert user signature with VBA
Description: This sample for both the built-in Outlook editor and WordMail demonstrates how to use CommandBars techniques to insert a known, named signature into the current email message. In particular, it demonstrates that WordMail in Outlook 2002/3 uses a bookmark named _MailAutoSig to handle signatures. See notes below for known issues.
Date: 26-Oct-2004  10:22
Code level: advanced
Code area: Outlook Expert Techniques
Posted by: Sue Mosher
This message is displayed as VB.NET
 Sub InsertSig(strSigName As String)
    Dim objItem As Object
    Dim objInsp As Outlook.Inspector
    ' requires a project reference to the
    ' Microsoft Word library
    Dim objDoc As Word.Document
    Dim objSel As Word.Selection
    ' requires a project reference to the
    ' Microsoft Office library
    Dim objCB As Office.CommandBar
    Dim objCBP As Office.CommandBarPopup
    Dim objCBB As Office.CommandBarButton
    Dim colCBControls As Office.CommandBarControls
    On Error Resume Next
    Set objInsp = Application.ActiveInspector
    If Not objInsp Is Nothing Then
        Set objItem = objInsp.CurrentItem
        If objItem.Class = olMail Then  ' editor is WordMail
            If objInsp.EditorType = olEditorWord Then
                ' next statement will trigger security prompt
                ' in Outlook 2002 SP3
                Set objDoc = objInsp.WordEditor
                Set objSel = objDoc.Application.Selection
                If objDoc.Bookmarks("_MailAutoSig") Is Nothing Then
                    objDoc.Bookmarks.Add Range:=objSel.Range, Name:="_MailAutoSig"
                End If
                objSel.GoTo What:=wdGoToBookmark, Name:="_MailAutoSig"
                Set objCB = objDoc.CommandBars("AutoSignature Popup")
                If Not objCB Is Nothing Then
                    Set colCBControls = objCB.Controls
                End If
            Else ' editor is not WordMail
                ' get the Insert | Signature submenu
                Set objCBP = Application.ActiveInspector.CommandBars.FindControl(, 31145)
                If Not objCBP Is Nothing Then
                    Set colCBControls = objCBP.Controls
                End If
            End If
        End If
        If Not colCBControls Is Nothing Then
            For Each objCBB In colCBControls
                If objCBB.Caption = strSigName Then
                    objCBB.Execute ' **** see remarks
                    Exit For
                End If
        End If
    End If
    Set objInsp = Nothing
    Set objItem = Nothing
    Set objDoc = Nothing
    Set objSel = Nothing
    Set objCB = Nothing
    Set objCBB = Nothing
End Sub
All 131comments
Page [ 1 2 3 4 5 6 7 8 9 10 Next >>  
  26-Oct-2004  17:25   

1) This code is for Outlook 2002/2003 VBA only. Note that in Outlook 2002 SP3, it will trigger a security prompt.

2) It requires references in Tools | References to the Office and Word libraries.

3) **** The operation of the built-in Outlook editor seems inconsistent. Sometimes it works fine and sometimes it inserts the last signature used, rather than the named signature specified. I'm baffled by this.

4) Usage:

Sub InsertMySig()
    Call InsertSig("Name of your signature")
End Sub

where "Name of your signature" is the exact name of the signature as it appears on the signature menus and dialogs.
  24-Nov-2004  14:08   
I recently wrote a simple macro for a group of users at my firm who need particular text added to any email message they send to the group. I gave them each custom signatures because the text needed to be formatted when using RTF or HTML. My macro is in the Application_ItemSend macro. To insert the signature if the message passes the criteria I used the code

Item.GetInspector.CommandBars.Item("Insert").Controls("Signature").Controls("Confidential MacroInsert").Execute

This works perfectly on my computer but, of course, has a problem when installed elsewhere. On my computer the "signature" is inserted at the beginning of the body, before the text. Not the usually place for a signature I know but exactly what I needed. On other users' computers the signature is inserted at the end of the document. I can not find any settings that are different.

Any ideas on what might be different or how I can control where the signature is placed? It is firm policy that users can not use WordMail formats so the bookmark references in this email will not work for me.

  25-Nov-2004  08:29   
Outlook itself provides no way to set the insertion point in a message that isn't using WordMail. You could try using SendKeys to position it.
  04-Dec-2004  17:47   
It works great, but it comes out in a font I don't use. Just inserting signature with regular insert signature command does that too. Is there a line of code I can add to set the font to Comic Sans? I tried finding a property to do that, but it didn't work, perhaps because I had it attached to wrong object.
  04-Dec-2004  20:05   
Sounds like you need to edit your signature. Make sure it's in Comic Sans and has a blank line before it.
  05-Dec-2004  08:02   
The signature is formatted in the desired font and has a line in front of it. The signature seems to appear in whatever font the header appears in. Would like to have line of code to force it to my font.
  05-Dec-2004  08:48   
It's not something you can do in a single line of code. If you're using Outlook's built-in email editor, you'd need to use the SafeInspector object from the Redemption library ( ). If you're using Word as your email editor, you have the option of recording a macro that runs in Word VBA.
  17-Jan-2005  14:22   
Try this (it works only with Outlook 2002):
Uses registry to find the Outlook Signature file and then reads it itno a string

Function GetOutlookSignature() As String

    Dim oShell As Object
    Dim bRegKey

    On Error GoTo ErrHandler
    '-- Read from Registry the Default Signature Name
    Set oShell = CreateObject("WScript.Shell")
    bRegKey = oShell.RegRead("HKCU\Software\Microsoft\Office\10.0\Common\MailSettings\NewSignature")

    '-- Get Application Data Directory
    Dim sAppDataDir As String
    sAppDataDir = oShell.SpecialFolders("AppData")

    '-- Read the Default Signature from a file
    Dim strSignaturePath As String, strHTML As String

    strSignaturePath = sAppDataDir + "\Microsoft\Signatures\" + bRegKey + ".htm"

    Const ForReading = 1, ForWriting = 2
    Dim fso As Object, f As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.OpenTextFile(strSignaturePath, ForReading)
    strHTML = f.ReadAll
    '-- Parse out Body
    Dim iStart As Integer, iEnd As Integer
    iStart = InStr(LCase(strHTML), "<body>") + Len("<Body>")
    iEnd = InStr(LCase(strHTML), "</body>")
    MsgBox strHTML
    GetOutlookSignature = "<br><br>" + Mid(strHTML, iStart, iEnd - iStart)

    MsgBox GetOutlookSignature
    GoTo ExitHandler
    MsgBox Err.Description, vbCritical, "GetOutlookSignature"
    Set oShell = Nothing
    Set fso = Nothing
    Set f = Nothing
End Function
  27-Jan-2005  07:02   
Does anyone know how to insert a date in a mail message?
I would like to have a new message created with today's
date at the top of the e-mail and a Signature at the bottom.

Any ideas?

Line 1 : 27 January
Line 2:

Line n: Signature

  27-Jan-2005  08:32   
Jerry, you didn't say anything about the version of Outlook or whether you're using WordMail as the email editor or what format messages you want to support. All of these things matter.
Page [ 1 2 3 4 5 6 7 8 9 10 Next >>