Tadwick

I have an Outlook 2003 add-in, developed w/ VSTO SE and written in C#. The add-in creates a command bar with two buttons on it. When the add-in shuts down and ThisAddIn_Shutdown is called, I save the command bar position using the approach described in http://msdn2.microsoft.com/en-us/library/ms268891(VS.80).aspx (modified for VSTO SE). The add-in runs fine in Outlook 2003. However, when I run the add-in in Outlook 2007 an exception is thrown (HRESULT 0x800A01A8) in ThisAddIn_Shutdown when I try to reference the properties of the CommandBar. I believe this suggests the object is out of scope.

From what I've read I assumed that, since I am not using any of the new Outlook 2007 OM, my code would work find in both Outlook 2003 and 2007.

Can anyone explain why the two versions of Outlook are treating the CommandBar object differently

Thanks, Tad

P.S. If the recommendation is to compile my code twice (for Outlook 2003 and then 2007) is there a good write-up of VS 2005 project architecture that explains how to do create two setup projects that share the same base



Re: Visual Studio Tools for Office Problem with ThisAddIn_Shutdown and CommandBar in Outlook 2007

X4U

Hello Tad,

The commandBar and VommandBarbutton variables should be declared within the class.

Where do you have declared the variables

Can you show us a codesnippet

Greets, Helmut






Re: Visual Studio Tools for Office Problem with ThisAddIn_Shutdown and CommandBar in Outlook 2007

Tadwick

Hi Helmut,

Here is a snippet

Code Snippet

using System;

using System.Windows.Forms;

using Microsoft.VisualStudio.Tools.Applications.Runtime;

using Outlook = Microsoft.Office.Interop.Outlook;

using Office = Microsoft.Office.Core;

namespace fooAddIn

{

public partial class ThisAddIn

{

Office.CommandBar fooAddInToolBar;

Office.CommandBarButton btn1;

Office.CommandBarButton btn2;

Outlook.Explorers selectExplorers;

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

{

try

{

Properties.Settings.Default["fooAddInToolBarTop"] = fooAddInToolBar.Top;

Properties.Settings.Default["fooAddInToolBarLeft"] = fooAddInToolBar.Left;

Properties.Settings.Default["fooAddInToolBarVisible"] = fooAddInToolBar.Visible;

Properties.Settings.Default["fooAddInToolBarPosition"] = (int)fooAddInToolBar.Position;

Properties.Settings.Default["fooAddInToolBarRowIndex"] = fooAddInToolBar.RowIndex;

Properties.Settings.Default.fooAddInIsLoaded = false;

Properties.Settings.Default.Save();

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

}

}

}





Re: Visual Studio Tools for Office Problem with ThisAddIn_Shutdown and CommandBar in Outlook 2007

Andrew Cherry [MSFT]

Hi Tad -

At first glance, that code looks like it should work. Can you provide where you're setting the fooAddInToolBar reference It's possible that object has gone away.

Thanks,

Andrew






Re: Visual Studio Tools for Office Problem with ThisAddIn_Shutdown and CommandBar in Outlook 2007

Tadwick

Hi Andrew,

Is this what you mean Any idea why it should be in scope for Outlook 2003 and not 2007

Tad

Code Snippet

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

{

selectExplorers = this.Application.Explorers;

selectExplorers.NewExplorer += new Outlook.ExplorersEvents_NewExplorerEventHandler(newExplorer_Event);

AddToolbar();

//Define event handler for Outlook's options pages to add custome property page

this.Application.OptionsPagesAdd += new Outlook.ApplicationEvents_11_OptionsPagesAddEventHandler(ThisAddIn_OptionsPagesAdd);

}

private void newExplorer_Event(Outlook.Explorer new_Explorer)

{

((Outlook._Explorer)new_Explorer).Activate();

fooAddInToolBar = null;

AddToolbar();

}

private void AddToolbar()

{

if (fooAddInToolBar == null)

{

Office.CommandBars cmdBars =this.Application.ActiveExplorer().CommandBars;

fooAddInToolBar = cmdBars.Add("fooAddInToolBarName",Office.MsoBarPosition.msoBarTop, false, true);

}

}





Re: Visual Studio Tools for Office Problem with ThisAddIn_Shutdown and CommandBar in Outlook 2007

Andrew Cherry [MSFT]

Hi Tad -

I think I've established what's happening with your solution, as well as the difference between Outlook 2003 and 2007.

It's a question of reference counting. Outlook 2007 made many improvements in terms of object lifetime and memory management. For the end user, it means that when you click "Quit," in most cases Outlook will shut down, and not remain running in the background as it was wont to do in Outlook 2003 (one of the reasons why the first VSTO Application level Add-in was for Outlook). For the developer, it means that sometimes Outlook is overly aggressive about releasing objects. As evidenced by the fact that you're getting an HRESULT-based exception versus a standard exception, Outlook is being a "good" COM client; it's not destroying the object from underneath you, which almost certainly would cause an outright memory access violation, and would be a violation of the rules of COM. Instead, Outlook is orphaning the object you have a reference to, preventing a crash while at the same time allowing Outlook to shut down when the end user requests it.

The basic problem is that as far as Outlook the application is concerned, the Explorer has gone away, therefore all associated CommandBars have been released. So despite your reference to the CommandBar, the Explorer's closing releases all the associated CommandBar(s). Maintaining a reference to the Explorer window won't help either; you need to read from the CommandBar before the Explorer is destroyed.

The basic issue is when you make the read of your settings.

I have two suggestions, both using the Explorer.Close event (only off the Outlook.Explorer_10_Events interface, not Explorer):

  1. Set Properties.Settings.Default["xyx"] = foo.Value in the CloseEventHandler; call Save in the Shutdown.
  2. Cache each of the values you want to save to appropriate types; continue writing them in Shutdown as you were.

You might be able to get more specific assistance from Outlook with regards to a better option of Event to handle (maybe check to see how many Explorers are left and only save the settings on the last close ), but this should put you on the right direction.

HTH,

Andrew






Re: Visual Studio Tools for Office Problem with ThisAddIn_Shutdown and CommandBar in Outlook 2007

Tadwick

Hi Andrew,

Just saw this - alerts on the thread no longer working apparently for the OP. Anyway, thanks for the detailed explanation and the suggested work around. I will try this over the next few days and report back. Thanks again for taking the time to look into this.

Tad





Re: Visual Studio Tools for Office Problem with ThisAddIn_Shutdown and CommandBar in Outlook 2007

Tadwick

Andrew,

I tried to implement Explorer.Close() and the close event isn't firing. Here's what I used:

Code Snippet

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

{

Outlook.Explorer expl = this.Application.ActiveExplorer();

Outlook.ExplorerEvents_10_Event explEvents = (Outlook.ExplorerEvents_10_Event)expl;

explEvents.Close += new Outlook.ExplorerEvents_10_CloseEventHandler(explEvents_Close);

}

private void explEvents_Close()

{

MessageBox.Show("closing active explorer");

}

Is this the proper syntax

I also tried

Code Snippet

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

{

Outlook.Explorer expl = this.Application.ActiveExplorer();

Outlook.ExplorerClass myExpl = null;

myExpl = (Outlook.ExplorerClass)expl;

myExpl.ExplorerEvents_10_Event_Close +=new Microsoft.Office.Interop.Outlook.ExplorerEvents_10_CloseEventHandler(myExpl_ExplorerEvents_10_Event_Close);

}

private void myExpl_ExplorerEvents_10_Event_Close()

{

MessageBox.Show("closing active explorer");

}

Its event didn't fire either.

Thanks, Tad





Re: Visual Studio Tools for Office Problem with ThisAddIn_Shutdown and CommandBar in Outlook 2007

Andrew Cherry [MSFT]

Hi Tad -

I would suggest maintaining a class member referring to the Outlook.Explorer, rather than just a stack variable.

It's possible the Explorer reference is being gc'd, and with it the attached events. I don't have a machine with me at the moment to check your code, but it does look right. I'll try it when I get into the office.

Andrew






Re: Visual Studio Tools for Office Problem with ThisAddIn_Shutdown and CommandBar in Outlook 2007

Tadwick

Hi Andrew,

Thanks for the additional info. Hope you get a chance to test the code on your machine.

Tad





Re: Visual Studio Tools for Office Problem with ThisAddIn_Shutdown and CommandBar in Outlook 2007

Andrew Cherry [MSFT]

Hi Tad -

I verified that maintaining a reference to the Outlook.Explorer object caused the event to fire properly, while not maintaining the reference resulted in the event not firing.

Andrew






Re: Visual Studio Tools for Office Problem with ThisAddIn_Shutdown and CommandBar in Outlook 2007

Tadwick

Andrew,

That's great news - would you be able to share a snippet to show the syntax

Thanks again for all your testing on this,

Tad





Re: Visual Studio Tools for Office Problem with ThisAddIn_Shutdown and CommandBar in Outlook 2007

Andrew Cherry [MSFT]

Code Snippet

public partial class ThisAddIn

{

private Outlook.Explorer expl;

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

{

expl = this.Application.ActiveExplorer();

((Outlook.ExplorerEvents_10_Event)expl).Close += new Microsoft.Office.Interop.Outlook.ExplorerEvents_10_CloseEventHandler(ThisAddIn_Close);

}

void ThisAddIn_Close()

{

MessageBox.Show("Test");

}

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

{

}

#region VSTO generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InternalStartup()

{

this.Startup += new System.EventHandler(ThisAddIn_Startup);

this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);

}

#endregion

}






Re: Visual Studio Tools for Office Problem with ThisAddIn_Shutdown and CommandBar in Outlook 2007

Tadwick

Andrew,

Thank for the snippet. Did you try this in OL2003 or just OL2007

Tx, Tad





Re: Visual Studio Tools for Office Problem with ThisAddIn_Shutdown and CommandBar in Outlook 2007

Tadwick

Andrew,

FYI - I finally got time to take another crack at this and found that I had a reference error that was not getting picked up by the compiler. So, all is good and thank you again for all the above contributions to this post.

Tad