Search code examples
c#outlookoffice-interopoutlook-addin

Modifying ActiveInlineResponseWordEditor from a Outlook 2007 add-in


I have an Outlook 2007 plugin developed that on the Application.ItemSend event makes a small change to each hyperlink in the email. This is accomplished by getting the Inspector.WordEditor property of the active inspector and looking through the Hyperlinks property.

Unfortunately since the introduction of Office 2013, this method does not work with the inline response feature meaning quick replies are ignored.

I am making modifications to this plugin to try and make this work but I am encountering some strange behaviour.

When running locally, both with and without the debugger, I can use reflection to get the ActiveInlineResponseWordEditor property of Application.ActiveExplorer() and make the same changes and all looks good.

When I package this solution and install it on a test machine, the code still executes are expected (I can see this from some logging I am doing), the changes I make are not actually persisted in the sent email – only for inline responses, all the other functionality works correctly.

I get access to a document using the following snippet:

Word.Document doc = null;

var explorer = Application.ActiveExplorer();
var wrapper = InspectorWrapper.GetWrapperFor(mailItem.GetInspector, logger);

try
{
    doc = (Word.Document)explorer.GetType().GetProperty("ActiveInlineResponseWordEditor").GetValue(explorer, null);
}
catch (TargetInvocationException) { /*Silently fail */ }

if (doc == null)
{
    if (wrapper is MailItemWrapper)
    {
        doc = wrapper.Inspector.WordEditor as Word.Document;
        logger.Log("Have inspector document.");
    }
}
else
{
    logger.Log("Have in-line document.");
}

And I use this document to modify all links which is done using this snippet:

foreach (Word.Hyperlink link in doc.Hyperlinks)
{
    var uriBuilder = new UriBuilder(link.Address);

    var query = HttpUtility.ParseQueryString(uriBuilder.Query);
    query.Set("id", 1);

    uriBuilder.Query = query.ToString();

    var newLink = uriBuilder.ToString();

    logger.Log(string.Format("{0} to {1}", link.Address, newLink));

    link.Address = newLink;
}

I have tried to just access the MailItem.GetInspector property alone, but the same symptoms exit. I have also tried calling mailItem.Save() after completing the changes, again the symptoms exist.

My hunch is that I am not getting a valid reference to the Word editor and thus the changes are not persisted but does anyone else have any ideas?


Solution

  • Try to read the HTMLBody of the outgoing message, load it into an IHTMLDocument object, modify the links, then reset the HTMLBody property.