Search code examples
vbaoutlook

VBA Detect if an e-mail in editing mode has a signature included?


I'm trying to add a signature but ensure that no other signature can be added after it. My method of adding it is basically concatenating the HTMLBody and a custom HTML-based signature.

As background, when I start writing an Outlook e-mail, I can attach a signature to it using Message → Include → Signature and picking the signature I need. However, no matter how many times I pick a signature, I won't get duplicates.

How does Outlook detects that a signature has been included? Is it a property under the MailItem object or the Inspector?

Thanks,


Solution

  • There is a bookmark when a user applies a signature.

    Option Explicit
    
    Private Sub signatureText()
    
    Dim newEmail As mailItem
    Dim olInsp As Inspector
    
    Dim wdDoc As Object
    Dim oRng As Object
    Dim oBookmark As Object
    
    Dim sigText As String
    
    Set newEmail = CreateItem(olMailItem)
        
    With newEmail
    
        Set olInsp = .GetInspector
        Set wdDoc = olInsp.WordEditor
        Set oRng = wdDoc.Range
        
        ' Default signature
        ' or set a breakpoint to manually add a signature
        .Display
        
        If wdDoc.Bookmarks.Exists("_MailAutoSig") Then
            Set oBookmark = wdDoc.Bookmarks("_MailAutoSig")
            
            sigText = oBookmark.Range.Text
            
            Debug.Print oBookmark.Range.Text
            MsgBox "Please do not add your own signature."
            
        Else
            Debug.Print "No signature bookmark."
        End If
        
    End With
        
    End Sub