Very Odd Behavior - Using a form on Outlook 2003 AddIn

M

Miguel Lopes

Hi,

I am developing a Outlook 2003 Add In, using Visual Studio 2005 and Shared
Add In project template with C#.
The Primary Interop Assemblies for Office 2003 are also installed.
The goal of the addin is to provide the user that sends an email the ability
to save it to disk. For that i am trapping the ItemSend
event as follows:

public void OnConnection(object application, Extensibility.ext_Co....
{
.....
Outlook._Application obj = (Outlook._Application)application;
obj.Application.ItemSend += new
ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
.....
}

In order to provide user interaction with the addin, i added to the project
a standard Windows Form ( WhereToSaveForm.cs )
and in the addin event handler code i put the following code:

MessageBox.Show("Saving");
WhereToSaveForm formSave = new WhereToSaveForm();
formSave.ShowDialog();

In order to test, i just added a button to the form, with a click event
handler, that justs closes the form:

this.Close();

When i send an email the following happen:

1) The message box is shown, saying: "Saving";
2) The form is loaded and shown;
3) Click on form button;
4) The form is closed;
5) Email is sent;

But when i repeat the procedure ( without closing Outlook ):

1) The message box is not shown;
2) The form is not loaded and obviously not shown;
3) The email is sent;

It seems that for some reason my addin gets unloaded or somehow the event
handler is not invoked.

I tracked down the problem to the following line:

formSave.ShowDialog();

if i comment this line, the event is always called, no matter how many
messages are sent ( the message box is always shown ).

In order to discover the problem i tested the following:

1) See if any exception was thrown that could cause the add in to unload -
No exception was thrown;
2) Try to reproduce the implementation of the message box, since with
MessageBox object the event is always fired - Without success;
3) Use Dispose() and Close() from the addIn ItemSend event handler, to get
the form closed - Without success;

Any ideas on this odd behavior?

Best Regards,
Miguel Lopes
 
K

Ken Slovak - [MVP - Outlook]

When it doesn't fire is the addin actually disconnected? You can check that
in the COM Add-Ins dialog on the Advanced Options page of the Tools, Options
dialog. You can also check in Help, About, Disabled Items.

I think it might be more likely that your handlers are going out of scope.
Are you adding them to a hashtable or declaring them with a scope that will
let them live for as long as Outlook is running?
 
M

Miguel Lopes

Hi Ken,

I am using the setup project that VS 2005 builds for me to install the
AddIn. When i click Install on the project the add in gets installed, but if
i open Tools -> Advanced Options -> COM Add Ins, the add in does't appear on
the list. Isn't that strange? The Disabled Items list is also empty, before
and after i run the add in for the first time.
Can be a problem related to the install process?

The invocation of the ShowDialog() method can put my handlers out of scope?
If i comment that line, everything works fine.
Here's how i setup the handlers:

public void OnConnection(object application, Extensibility.ext_ConnectMode
connectMode, object addInInst, ref System.Array custom)
{
applicationObject = application;
addInInstance = addInInst;

Outlook._Application obj = (Outlook._Application)application;
obj.Application.ItemSend += new
ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
}

I configured the add in to load when the host application starts.
 
K

Ken Slovak - [MVP - Outlook]

If your code is running it shouldn't matter. It might be installed in HKLM
for all users in which case it won't show up in the COM Add-Ins dialog.

I set up Item_Send handlers the same way with no problems, it must be
showing that dialog. I'm not sure why that would either cause an exception
or cause the event handler to stop working so this is all guesses. I would
try putting the display code within a try...catch block and see if anything
turns up. Other than that I have no idea why the problem is happening. Have
you tried a C# newsgroup to see if anyone there has any ideas?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top