none
assign signature based on SentOnBehalfName and remove existing signature RRS feed

  • Question

  • Hi,

    I'm using windows 7 - 32-bit + outlook 365 with an exchange account and an additional shared mailbox (which for security reasons cannot be added as a "real" account)

    I'm able to successfully add signature when SentOnBehalfName is not empty. If it's empty it's being sent from main email account in Outlook and a different signature does not need to be added. Problem is by default a signature is assigned to main account.

    Easiest for me would be if user would not have signature assigned to new emails or replies but my guess is that users don't trust that when clicking send a signature will get attached so I would prefer to keep their default signature visible and remove it after Itemsend event. I know there is a method when creating a new mailitem to remove _MailAutoSig . But in the Itemsend event word html layout is converted so _MailAutoSig is no longer available. Now my best option would be to remove the already present signature by removing the span elements that contains the signature.

    Could someone tell me what pattern I need to use where you would only catch spans that have "Calibri Light" as their font-family ?

    And maybe a pattern to replace 1st hr occurence in reply email to have it replaced by signature + hr ?

    Code I have now(for debugging purposes I'm dumping htmlbody to txt to view html output):

    Option Explicit
    
    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
        Dim propertyAccessor As Outlook.propertyAccessor
        Dim fromSenderEmail As String
        Dim strSigname As String
        Dim SentOnBehalfOfName As String
        Dim FMBaddress As String
        Dim IMBsignature As String
        Dim FMBsignature As String
    
        FMBaddress = "censored"
        IMBsignature = "tim"
        FMBsignature = "fmb"
    
        SentOnBehalfOfName = "http://schemas.microsoft.com/mapi/proptag/0x0065001F"       
    
        If Item.Class = olMail Then
          Set propertyAccessor = Item.propertyAccessor
          fromSenderEmail = propertyAccessor.GetProperty(SentOnBehalfOfName)
    End If
    
        Select Case fromSenderEmail
            Case "" 'email being sent from default mail account in Outlook
                strSigname = IMBsignature
            Case FMBaddress
                strSigname = FMBsignature
        End Select
    
        If strSigname <> "" Then
            InsertSignature Item, strSigname
        End If
    End Sub
    
    Private Sub InsertSignature(olkMsg As Outlook.MailItem, strSigname As String)
        Dim objFSO As Object, objShell As Object, objSignatureFile As Object, strSigFilePath As String, strBuffer As String, TS As TextStream
        Set objShell = CreateObject("Wscript.Shell")
        strSigFilePath = objShell.SpecialFolders("Desktop")
        strSigFilePath = Replace(strSigFilePath, "Desktop", "AppData\Roaming\Microsoft\Signatures\")
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set TS = objFSO.CreateTextFile("c:\temp\emailbody.txt", True)
    
        Select Case olkMsg.BodyFormat
            Case olFormatHTML
                Set objSignatureFile = objFSO.OpenTextFile(strSigFilePath & strSigname & ".htm")
                strBuffer = objSignatureFile.ReadAll
                DeleteSig olkMsg
                olkMsg.HTMLBody = olkMsg.HTMLBody & "<br />" & strBuffer
                TS.WriteLine (olkMsg.HTMLBody)
                TS.Close
            Case olFormatPlain
                Set objSignatureFile = objFSO.OpenTextFile(strSigFilePath & strSigname & ".txt")
                strBuffer = objSignatureFile.ReadAll
                olkMsg.Body = olkMsg.Body & strBuffer
        End Select
    
        objSignatureFile.Close
        Set objSignatureFile = Nothing
        Set objFSO = Nothing
        Set objShell = Nothing
        Set olkMsg = Nothing
    End Sub
    
    Private Sub DeleteSig(olkMsg As Outlook.MailItem)
        Dim objRegEx As Object, strPattern As String, strReplace As String
        Set objRegEx = CreateObject("vbscript.regexp")
        strPattern = "<span [^>]*""Calibri Light""(.*?)<\/span>"
        strReplace = ""
    
        With objRegEx
            .Global = True
            .IgnoreCase = False
            .Pattern = strPattern
        End With
    
        olkMsg.HTMLBody = objRegEx.Replace(olkMsg.HTMLBody, strReplace)
    End Sub
    Monday, June 18, 2018 12:11 PM

All replies

  • So I went this route

            '* Tools->References "Microsoft HTML Object Library"
            Dim oHtml4 As MSHTML.IHTMLDocument4
            Set oHtml4 = New MSHTML.HTMLDocument
    
            Dim oHtml As MSHTML.HTMLDocument
            Set oHtml = Nothing
    
            Set oHtml = oHtml4.createDocumentFromUrl(sLocalFilename, "")
    
            '* need to wait a little whilst the document parses
            '* because it is multithreaded
            While oHtml.readyState <> "complete"
                DoEvents  '* do not comment this out it is required to break into the code if in infinite loop
            Wend
            Debug.Assert oHtml.readyState = "complete"
    
            Dim htmlAnswers As Object 'MSHTML.DispHTMLElementCollection
            Set htmlAnswers = oHtml.querySelectorAll("p.MsoNormal > span[style*=""COLOR: #333399""]")

    How can I remove all items/nodes that I selected this way 

    BTW: for some reason when using querySelectorAll the style attribute has it's css property in uppercase while in the original html file the css property is lower case. Any reason for this ?
    Monday, June 18, 2018 7:41 PM
  • Short summary:

    1) get sentonbehalfofname

    2) if empty leave it like it is (it's being sent with main account email address)

    3) as _MailAutoSig is no longer available after itemsend event is treated we have to check html code (luckily signature is 12pt calibri light color #333399)

    4) what I want to in html: remove all p tags with MsoNormal class that contain span elements with font-family 'calibri light' property as well as empty (or contain &nbsp;) p tags from MsoNormal class

    5) use this modified html in htmlbody of item object to send out modifiend MailItem

    Monday, June 18, 2018 9:05 PM