Determine if default signature is used in email body

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

Determine if default signature is used in email body
Outlook add-in to determine if default signature is used in email body 
Johannes Drechsler




Posts: 11
Joined: 2016-08-01
Hello,

I am developing an add-in for Outlook 2010 that detects if an email is sent to an external contact. If that is the case the email needs to contain a signature.

Is there a way to detect if an email contains a signature or not? I tried using MailItem.Body and MailItem.HTMLBody but they contain the whole email body. I only want my own email body.

I know that the default signature in Outlook is called "_MailAutoSig". Is there a way to extract this region from the email body so I can compare it to the signature it should contain? I hope it is clear what I am trying to achieve.

Thank you in advance,

Johannes Drechsler
Posted 01 Aug, 2016 04:34:01 Top
Andrei Smolin


Add-in Express team


Posts: 18793
Joined: 2006-05-11
Hello Johannes,

You get Inspector.WordEditor and cast it to Word.Document. Then you set Document.Bookmarks.ShowHidden=true, retrieve the bookmark and get the range via Bookmark.Range. Finally, you get the text and optionally, the formatting and compare them with the text and formatting that you have.


Andrei Smolin
Add-in Express Team Leader
Posted 01 Aug, 2016 06:20:20 Top
Johannes Drechsler




Posts: 11
Joined: 2016-08-01
Thank you for your fast response!

I tried that but it does not seem to work.

My default signature text in Outlook is "This is my default signature."
I created a new email and changed the automatically added signature to "This is my changed default signature."
Then I hit send and jumped right to the method that should retrieve the changed signature text.

This is my used code:


Outlook.MailItem item = (Outlook.MailItem)OutlookApp.CreateItem(Outlook.OlItemType.olMailItem);
Outlook.Inspector inspector = item.GetInspector;
Word.Document document = (Word.Document)inspector.WordEditor;

document.Bookmarks.ShowHidden = true;
var bkmText = document.Bookmarks["_MailAutoSig"].Range.Text;


This method is called in the "adxOutlookEvents_ItemSend"-event

I only receive the normal default signature text (This is my default signature) but not the changed one.

Is there a way to retrieve the changed signature field text right when the user hits "send"?

Thank you for your help!
Posted 01 Aug, 2016 07:48:27 Top
Andrei Smolin


Add-in Express team


Posts: 18793
Joined: 2006-05-11
Johannes,

The code fragment provided creates a new item and retrieves the bookmark from the new item. Obviously, you need to retrieve the text from the item being sent, not from a newly created item.


Andrei Smolin
Add-in Express Team Leader
Posted 01 Aug, 2016 09:15:22 Top
Johannes Drechsler




Posts: 11
Joined: 2016-08-01
That is true!

I changed it so it uses the item that is being sent:


private void BookmarkTest(Outlook.MailItem item)
{
   Outlook.Inspector inspector = item.GetInspector;
   Word.Document document = (Word.Document)inspector.WordEditor;

   document.Bookmarks.ShowHidden = true;
   var bkmText = document.Bookmarks["_MailAutoSig"].Range.Text;
}


Now my problem is that it cannot find the bookmark anymore. What could be the problem?
Posted 02 Aug, 2016 05:34:00 Top
Andrei Smolin


Add-in Express team


Posts: 18793
Joined: 2006-05-11
Johannes,

Does this work if you turn all other COM add-ins off and restart Outlook? If yes, then the issue may be caused by leaving a number of COM objects that your code creates unreleased; see https://www.add-in-express.com/docs/net-office-tips.php#releasing.

Is the email created in the HTML format?


Andrei Smolin
Add-in Express Team Leader
Posted 02 Aug, 2016 08:42:23 Top
Johannes Drechsler




Posts: 11
Joined: 2016-08-01
I deactivated all other COM add-ins and restarted Outlook but it still cannot find the bookmark anymore. Yes it is created in HTML format.

Regards,

Johannes Drechsler
Posted 03 Aug, 2016 07:03:34 Top
Andrei Smolin


Add-in Express team


Posts: 18793
Joined: 2006-05-11
Hello Johannes,

Please create a new add-in project, add a Ribbon button to the MailCompose Ribbon, add an event handler to the Click event of the button and test this code:

private void adxRibbonButton1_OnClick(object sender, IRibbonControl control, bool pressed)
{
    object context = control.Context;
    if (context is Outlook._Inspector)
    {
        Outlook.Inspector insp = context as Outlook.Inspector;
        object item = insp.CurrentItem;
        if (item is Outlook._MailItem)
        {
            BookmarkTest(item as Outlook._MailItem);
        }
        Marshal.ReleaseComObject(item); item = null;
    }
    Marshal.ReleaseComObject(context); context = null;
}
private void BookmarkTest(Outlook._MailItem item)
{
    Outlook.Inspector inspector = item.GetInspector;
    Word.Document document = (Word.Document)inspector.WordEditor;
    Word.Bookmarks bkms = document.Bookmarks;

    bkms.ShowHidden = true;
    Word.Bookmark bkm = null;
    try
    {
        bkm = bkms["_MailAutoSig"]; 
    }
    catch (Exception ex)
    {
        // skip the exception
    }
    if (bkm != null)
    {
        Word.Range bkmRange = bkm.Range;
        var bkmText = bkmRange.Text;
        if (string.IsNullOrWhiteSpace(bkmText))
            MessageBox.Show("Empty");
        else
            MessageBox.Show(bkmText);
        Marshal.ReleaseComObject(bkmRange); bkmRange = null;
        Marshal.ReleaseComObject(bkm); bkm = null;
    }
    else
    {
        MessageBox.Show("No bookmark");
    }
    Marshal.ReleaseComObject(bkms); bkms = null;
    Marshal.ReleaseComObject(document); document = null;
    Marshal.ReleaseComObject(inspector); inspector = null;
} 



Andrei Smolin
Add-in Express Team Leader
Posted 05 Aug, 2016 07:22:44 Top
Johannes Drechsler




Posts: 11
Joined: 2016-08-01
Hello Andrei,

thank you! This worked like a charm. I want to receive the bookmark when the user clicks on send and not before. Is that possible as well?

Thank you in advance!
Posted 12 Aug, 2016 11:19:11 Top
Andrei Smolin


Add-in Express team


Posts: 18793
Joined: 2006-05-11
Hello Johannes,

You need to handle the Application.ItemSend event. In Add-in Express, this event is mapped to the ItemSend event of the Outlook Events component (ADXOutlookAppEvents).


Andrei Smolin
Add-in Express Team Leader
Posted 15 Aug, 2016 05:15:51 Top