Wednesday, January 09, 2008

Maintaining Custom Toolbar Positions in Outlook 2007

NOTE: This applies to Visual Studio 2008 and Outlook 2007.

There are a few articles floating around have examples on saving toolbar position information at the end of a session and restoring that position the next time Outlook is loaded. One at MSDN applies to Visual Studio 2008 and .NET Framework 3.5:

How to: Maintain Position Information for Custom Toolbars between Outlook Sessions

I tried this code with Outlook 2007 and it throws an exception on shutdown when it tries to save the toolbar position information. Reading the article again, I noticed that it says it applies to Outlook 2003.

I just assumed that it would work with Outlook 2007, and why would they release an article for Visual Studio 2008 that works with an old version of Outlook?

The exception that is being thrown when the CommandBar instance is accessed is:

System.Runtime.InteropServices.COMException: Exception from HRESULT: 0x800A01A8

Look up this HRESULT value and you can see that it means "Object Required".

To me, this means that the CommandBar has already been cleaned up and is no longer accessible. ThisAddIn_Shutdown is supposedly the first user code to be run before a shutdown occurs, so what do you do?

I have settled on listening to the Close event of the main Explorer window (the one the CommandBar is added to). This is called before ThisAddIn_Shutdown and the CommandBar is still accessible.



private void ThisAddIn_Startup(object sender, System.EventArgs e)
Outlook.Explorer explorer = this.Application.ActiveExplorer();

if (explorer != null)
((ExplorerEvents_10_Event)explorer).Close += new ExplorerEvents_10_CloseEventHandler(ThisAddIn_Close);

commandBar = explorer.CommandBars.Add(TOOLBARNAME,
Office.MsoBarPosition.msoBarFloating, false, true);


private void ThisAddIn_Close()



Adrian Brown said...

Apparently there was/is some reference counting issues in Outlook 2003, where some objects were not being cleaned up correctly.

Outlook 2007 has attempted to address this issue, hence the changes in behaviour.

Anonymous said...

Hi Adrian,
I love the code BUT when I use the beforemove it catches an appointment being dragged to a new start/end. After I let it through the move code it correctly sits in the dragto position and the summary shows the new times but it doesn't fire a item-change event so I can't update the control database the appointments relate to. The next time it refreshes from the database it puts it back to the old position. Help!!!!

Leon Victor said...

The following steps show how to generate a toolbar:
1. Select Customize from the Tools menu.
2. Select the Toolbars tab and click New.
3. Enter a name for the Toolbar and click OK.
4. The toolbar appears as an empty box.
5. Add buttons and commands in the toolbar as explained in the beginning of Adding a Button or Command.

Anonymous said...

Wow Great information about toolbar. I like your blog post. If you want more information about Toolbar Developer then visit our website.

Anonymous said...

Wow Great information about toolbar. Toolbar is best way to increase your internet work speed and also increase online profit. Custom toolbar increase your online reputation and branding also. Learn more about Toolbar development then visit our website.