Adding extra signature in any sent emails

Add-in Express™ Support Service
That's what is more important than anything else

Adding extra signature in any sent emails
 
franck DAMMANN




Posts: 41
Joined: 2021-01-26
Dear all,

I would like to add additional HTML text (a kind of signature) content into each email sent (just below customer's signature if there is one and not at the end of the email in the case of replies). I encounter 2 problems:

1- I tried to add my code into adxOutlookAppEvents1_NewInspector but: If outlook has been configured to include a signature, my own extra signature is erasing the outlook one...
I'm first looking for existing Bookmarks in the mail before adding the extra one but it seems that adxOutlookAppEvents1_NewInspector event is called before the insertion of the outlook signature...
Hence my function is not detecting any bookmark and perform a simple mailItem.HTMLBody += "<br><br><br><div ..." and outlook signature is removed.

If I'm doing such function on ribbon button click, my extra signature is added correctly just after the "official one". this is the reason why I think the adxOutlookAppEvents1_NewInspector event is triggered before insertion of outlook signatures.

2- If I'm trying to do the same thing for inline email responses (adxOutlookAppEvents1_ExplorerInlineResponse) my additional signature is never included, I'm not able to detect any bookmark and my mailItem.HTMLBody += ... has no effect.


Here are the sample code used:

For the Bookmark detection:
private bool CheckForBookmarks(Outlook._MailItem item)
        {
            Word.Document document;
            Word.Bookmarks bkms;

            try
            {
                document = item.GetInspector.WordEditor;
                bkms = document.Bookmarks;
            }
            catch(Exception e)
            {
                Debug.WriteLine("Exception while retrieving bookmarks " + e.Message);
                return false;
            }
            bool isBookmark = false;

            bkms.ShowHidden = true;
            Word.Bookmark bkm = null;
            try
            {
                bkm = bkms["_MailAutoSig"];
            }
            catch (Exception ex)
            {
                // skip the exception
                Debug.WriteLine("Exception while retrieving bookmarks " + ex.Message);
            }
            if (bkm != null)
            {
                Word.Range bkmRange = bkm.Range;
                var bkmText = bkmRange.Text;
                if (string.IsNullOrWhiteSpace(bkmText))
                    Debug.WriteLine("Bookmark Empty");
                else
                {
                    Debug.WriteLine("Bookmark: " + bkmText);
                    isBookmark = true;
                }
                Marshal.ReleaseComObject(bkmRange); bkmRange = null;
                Marshal.ReleaseComObject(bkm); bkm = null;
            }
            else
            {
                Debug.WriteLine("No bookmark");
            }
            Marshal.ReleaseComObject(bkms); bkms = null;
            Marshal.ReleaseComObject(document); document = null;
            return isBookmark;
        }


and the addSignature function (called in the different adx events):

public void addSignature(Outlook._MailItem mailItem)
        {
            
            bool bookmarkPresent = CheckForBookmarks(mailItem);
            Word.Document doc;
            Word.Selection objSel;
                    try
                    {
                        doc = mailItem.GetInspector.WordEditor;
                        objSel = doc.Windows[1].Selection;
                        if (bookmarkPresent)
                        {
                            Debug.WriteLine("addSignatureReceiptEnd");
                            addSignatureReceiptEnd(mailItem);
                        }
                        else
                        {
                            Debug.WriteLine("addSignatureReceipt thanks to WordEditor");
                            addSignatureReceipt(objSel);
                        }
                        Marshal.ReleaseComObject(doc); doc = null;
                        Marshal.ReleaseComObject(objSel); objSel = null;
                    }
                    catch(Exception e)
                    {
                        Debug.WriteLine("Unable to get WordEditor !"+e.Message);
                        addSignatureReceiptEnd(mailItem);
                    }           
        }


and finally the two functions to add signature (using WordEditor if there is a Bookmark detected or at the end of the email if there is not

        public void addSignatureReceipt(Word.Selection objSel)
        {
            String html = "<br><br><br><span id="sigName">[i]test[/i]";
            Clipboard.SetData("HTML Format",string.Format("Version:0.9
StartHTML:80
EndHTML:{0,8}
Start" + "Fragment:80
EndFragment:{0,8}
", 80 + html.Length) + html + "<");
            objSel.Range.Paste();
        }

        public void addSignatureReceiptEnd(Outlook._MailItem mailItem)
        {
            mailItem.HTMLBody += "<br><br><br><span id="sigName">[i]test[/i]";
        }


If you are able to tell me how to trigger adx events after Outlook signature that will be great ! of may be adding a custom outlook signature ?

Thanks,
Franck.
Posted 13 Apr, 2021 07:10:09 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
Hello Franck,

I don't know when Outlook creates that bookmark. Note that it doesn't create it if you re-open the email stuck in the Outbox (because of settings or some other issue).

I would check if the bookmark is created when Outlook opens the item: in the NewInspector event you get the item being opened and connect to its Open event; you can use an Outlook Item (not Items!) Events class; see the Add New Item dialog of your add-in project; also check section Events classes in the PDF file in the folder {Add-in Express}\Docs on your development PC.

To all: note that the Open event (including MailItem.Open, AppointmentItem.Open, etc) is cancellable. This prevents the inspector window from opening. At this moment, you can show a form or dialog window of your own.


Andrei Smolin
Add-in Express Team Leader
Posted 14 Apr, 2021 02:18:16 Top