welikeike

Anytime I invoke MessageBox.Show() or [custom form].Show() or [custom form].ShowDialog(), the ThisAddIn.Application.Inspectors.NewInspector event no longer fires. Is there any easy fix

9/5/2007:

This is a general problem that can be easily reproduced in several different ways but I'll include a simple example.

Code Snippet

public partial class ThisAddIn

{

private void ThisAddIn_Startup(object sender, System.EventArgs e)

{

MessageBox.Show("howdy");

Application.Inspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);

}

private void Inspectors_NewInspector(Outlook.Inspector Inspector)

{

if (Inspector.CurrentItem is Outlook.MailItem)

{

Outlook.MailItem mail = (Outlook.MailItem)Inspector.CurrentItem;

mail.Subject = "new subject";

}

}

}

If the MessageBox.Show("howdy") line is commented out, any message that opens up gets "new subject" as the subject. If the message box is allowed to display, Inspectors_NewInspector is never invoked. The same is true if a custom form is shown in place of the message box. If the code to show a message box or form is within the Inspectors_NewInspector method, the message box or form will be shown once (Inspectors_NewInspector will be invoked once) but the next time an item is opened, Inspectors_NewInspector is not invoked.



Re: Visual Studio Tools for Office NewInspector no longer fires after UI involvement

Mike Morton - MSFT

Your event handler is getting garbage collected!

Change your startup code to something like this which keeps a reference to the Inspectors collection. I underlined the 2 new lines of code below. This will then keep your event handler from getting garbage collected.

Outlook.Inspectors allInspectors;

private void ThisAddIn_Startup(object sender, System.EventArgs e)

{

MessageBox.Show("howdy");

allInspectors = Application.Inspectors;

Application.Inspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);

}

Hope this helps,

Mike





Re: Visual Studio Tools for Office NewInspector no longer fires after UI involvement

welikeike

Incredible! It worked. I read some of the other posts about scoping a command button at the class level to avoid garbage collection (which obviously didn't apply here) but I never would have guessed the same would apply to Application.Inspectors. Should I assume the same is true for Application.Explorers

Thanks so much for your help!





Re: Visual Studio Tools for Office NewInspector no longer fires after UI involvement

Mike Morton - MSFT

Glad to help! Yes, this would be true for Explorers also if you were hooking up to the Explorers.NewExplorer event.

Mike





Re: Visual Studio Tools for Office NewInspector no longer fires after UI involvement

Veludo

private Outlook.Inspectors _allInspectors;

private void ThisAddIn_Startup(object sender, System.EventArgs e)

{

_allInspectors = Application.Inspectors;

// Attach an handler to run on each newly created inspector

Application.Inspectors.NewInspector += new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);

}

void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)

{

//_allInspectors.Add(Inspector);

if (Inspector.CurrentItem is Outlook.MailItem)

{

Outlook.MailItem mail = (Outlook.MailItem)Inspector.CurrentItem;

MessageBox.Show(mail.Subject);

}

}

I have this code and the problem persists...can you help me please ...just for the first email works ok...

Thanks.





Re: Visual Studio Tools for Office NewInspector no longer fires after UI involvement

Veludo

The solution is:

private Outlook.Inspectors _allInspectors;

private void ThisAddIn_Startup(object sender, System.EventArgs e)

{

_allInspectors = Application.Inspectors;

// Attach an handler to run on each newly created inspector

_allInspectors.NewInspector += new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);

}

void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)

{

//_allInspectors.Add(Inspector);

if (Inspector.CurrentItem is Outlook.MailItem)

{

Outlook.MailItem mail = (Outlook.MailItem)Inspector.CurrentItem;

MessageBox.Show(mail.Subject);

}

}