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 |
|
Sergey Grischenko
Add-in Express team
Posts: 7235
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);
}
}
|
|
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 |
|
Sergey Grischenko
Add-in Express team
Posts: 7235
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?
|
|
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 |
|
Sergey Grischenko
Add-in Express team
Posts: 7235
Joined: 2004-07-05
|
Hi Søren.
Another workaround is to get the active inspector from the inspector argument in the InspectorActivate event handler.
|
|