Howto release an OutlookApp reference from WordMail?

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

Howto release an OutlookApp reference from WordMail?
 
Søren Hjarlvig




Posts: 34
Joined: 2007-02-22
I'm developing a Outlook addin which adds a new commandbar with a single button in mail inspectors. If the button is pressed, the Outlook process does not exit when Outlook is closed.

The problem seems to be a reference to OutlookApp - if I do a Marshal.ReleaseComObject, the process exits as it should. However, doing so causes an exception the next time the user clicks the commandbar button.

I tried doing the cleanup in AddinBeginShutdown and it works, but not when using WordMail. I guess the reference must be created within Word, since the reference is always null in AddinBeginShutdown. Is Word running its own instance of the addin ?

I need OutlookApp in order to get the current active inspector - maybe there is another way to achieve this ?

I have the following code:

public AddinModule()
{
InitializeComponent();

this.adxAddFolderButton.Click +=
new ADXClick_EventHandler(adxAddFolderButton_Click);

this.AddinBeginShutdown += new
ADXEvents_EventHandler(AddinModule_AddinBeginShutdown);

}

private void adxAddFolderButton_Click(object sender)
{

//AddFolder addFolder = new AddFolder(); // application code

_Application outlookApp = this.OutlookApp;

//addFolder.AddFolder_Click(outlookApp.ActiveInspector()); // application code

// This makes Outlook shutdown clean, but the button can only be used once:
Marshal.ReleaseComObject(outlookApp);
outlookApp = null;

}

Best regards.

Søren Hjarlvig
Posted 06 Jun, 2007 16:15:40 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
Hi Søren.

Please don't release the OutlookApp at all. It is done by Add-in Express.
Also please use the addinmodule designer to add event handlers to the code.

To get the active inspector please use the following code:

private void adxCommandBarButton1_Click(object sender)
{
Outlook._Inspector activeInspector = null;

try
{
activeInspector = OutlookApp.ActiveInspector();
// TODO:
}
finally
{
if (activeInspector != null)
Marshal.ReleaseComObject(activeInspector);
}
}
Posted 07 Jun, 2007 08:13:35 Top
Søren Hjarlvig




Posts: 34
Joined: 2007-02-22
Thank you for your reply.

It seems to work, but only sometimes.
When I first tested your code it worked, but then it stopped working, and I didn't change anything - Very, very strange.

Best regards

Søren
Posted 08 Jun, 2007 06:35:45 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
Hi Søren.

Please review your code and determine what exactly line of the code produces the issue. Do you use Outlook 2000?
Posted 08 Jun, 2007 08:25:12 Top
Søren Hjarlvig




Posts: 34
Joined: 2007-02-22
Hi again,

No, I use Outlook 2003.
The exact line causing the problem is:

activeInspector = OutlookApp.ActiveInspector();

The problem seems to be connected to the use of the OutlookApp property. But only when using WordMail.

However, I found a solution using SendMessage:

private void adxAddFolderButton_Click(object sender)
{
SendMessage(WM_ADD_FOLDER_MESSAGE, IntPtr.Zero, IntPtr.Zero);
}

private void AddinModule_OnSendMessage(object sender, ADXSendMessageEventArgs e)
{
try
{
if (e.Message == WM_ADD_FOLDER_MESSAGE)
{
Inspector activeInspector = null;
try
{
activeInspector = OutlookApp.ActiveInspector();

AddFolder addFolder = new AddFolder(); // TODO addFolder.AddFolder_Click(OutlookApp.ActiveInspector()); // TODO
}
finally
{
if (activeInspector != null)
Marshal.ReleaseComObject(activeInspector);

activeInspector = null;
}
}
}
catch (Exception ex)
{
ErrorLogger.Log(ErrorLogger.ERROR_GENERAL, ex);
}
}

This way OutlookApp is only referenced from Outlook, where it is properly released.

Best regards

Søren Hjarlvig
Posted 11 Jun, 2007 05:01:23 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
Hi Søren.

Another workaround is to get the active inspector from the inspector argument in the InspectorActivate event handler.
Posted 11 Jun, 2007 08:49:43 Top