ezG
ezG

Reputation: 421

Outlook Crashing on Spellcheck when COM Add-In tries reading message

OUTLOOK VERSION: 2212 Build 15928.20282
32 Bit Outlook running on 64-Bit Win.

PRE-CONDITION:
Have a simple add-in which Overrides the InspectorActivate event handler.

RECREATE:
Open a new message Item
type in a few words and make a spelling error
Select the misspelled item, and LEFT click on one of the words suggested in the dropdown list.

When the event handler attempts to access the Body property of the Outlook MailItem a System.AccessViolation is thrown, and my exception handler is never reached.

mailItem = Inspector.CurrentItem as Outlook._MailItem;               

if (mailItem != null)
{
    try
    {
        if (mailItem.Body == null)  // System.AccessViolationException
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    catch(Exception e)
    {
       log.Error(e);
    }
 }

Upvotes: 0

Views: 273

Answers (3)

ezG
ezG

Reputation: 421

I implemented the following suggestion, and it's working

This looks like an issue in the Outlook object model. I suggest that introduce a small delay before executing the logic which is now written in InspectorActivate. To achieve this, use the SendMessage/OnSendMessage machinery that we describe in section Wait a little; see the PDF file in the folder {Add-in Express}\Docs on your development PC. When the OnSendMessage event occurs and you filter out your message, get ActiveInspector and execute your logic for that Inspector object.

For the code, look towards the bottom of the page Solution at Bottom of page

Upvotes: 0

Eugene Astafiev
Eugene Astafiev

Reputation: 49395

The How to handle AccessViolationException page explains how to handle such exceptions in the managed code starting from the .net framework 4.0.

You may also try using the HTMLBody property instead. If it gives the same results I'd suggest retrieving the WordEditor instance in that case. You can read more about all these ways in the Chapter 17: Working with Item Bodies.

The Body property of the MailItem class returns or sets a string representing the clear-text body of the Outlook item. To preserve any formatting you need to use the HTMLBody or the Word editor instance.

Upvotes: 1

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66255

Try to access the message body using the Word editor - worst case, you get an exception that you can handle:

var body = Inspector.WordEditor.Content.Text;

Upvotes: 1

Related Questions