Kulvinder

Hi,

I am creating an Outlook 2007 Addin using VSTO SE and my code is as follows :

Code Snippet

public partial class ThisAddIn

{

Outlook.Explorers myExplorers = null;

Office.CommandBar myCommandBar = null;

Office.CommandBarButton myCommandBarButton = null;

void Explorers_NewExplorer(Microsoft.Office.Interop.Outlook.Explorer Explorer)

{

((Outlook.ExplorerEvents_10_Event)Explorer).Activate += new Microsoft.Office.Interop.Outlook.ExplorerEvents_10_ActivateEventHandler(ThisAddIn_Activate);

LoadToolBar();

}

void ThisAddIn_Activate()

{

LoadToolBar();

}

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

{

myExplorers = this.Application.Explorers;

myExplorers.NewExplorer += new Microsoft.Office.Interop.Outlook.ExplorersEvents_NewExplorerEventHandler(Explorers_NewExplorer);

if (this.Application.Explorers.Count > 0)

{

LoadToolBar();

}

}

private void LoadToolBar()

{

try

{

foreach (Office.CommandBar bar in this.Application.ActiveExplorer().CommandBars)

{

if (bar.Name.Equals("Test"))

{

myCommandBar = bar;

break;

}

}

if (myCommandBar == null)

{

myCommandBar = this.Application.ActiveExplorer().CommandBars.Add("Test", missing, missing, true);

myCommandBar.Protection = Office.MsoBarProtection.msoBarNoProtection;

myCommandBar.Position = Microsoft.Office.Core.MsoBarPosition.msoBarTop;

myCommandBar.Left = 0;

myCommandBar.RowIndex = 0;

Office.CommandBarControl myCommandBarControl = myCommandBar.Controls.Add(Office.MsoControlType.msoControlPopup, missing, missing, missing, true);

myCommandBarControl.Caption = "Test";

myCommandBarButton = ((Office.CommandBarPopup)myCommandBarControl).CommandBar.Controls.Add(Office.MsoControlType.msoControlButton, 1, "", 1, true) as Office.CommandBarButton;

myCommandBarButton.Caption = "test";

myCommandBarButton.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(myCommandBarButton_Click);

}

myCommandBar.Visible = true;

}

catch(Exception ex)

{

MessageBox.Show("Error while Loading Toolbar : " + ex.Message + "\n" + ex.StackTrace);

}

}

void myCommandBarButton_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)

{

}

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

}

The problem i am facing is as follows :

I am using an application which automates my Outlook 2007 and when Outlook is not running and only my app is running, i see my toolbar when i open Outlook since i have added my toolbar at the New Explorer Activate event. Till this no problem. Now when i close my outlook and reopen it again, the toolbar doesnt appear. Instead, i get an error :

Error whiloe Loading Toolbar : Exception from HRESULT 0x800A01A8

at Microsoft.Office.Core.CommandBar.set_Visible(Boolean )

at ThisAddin.LoadToolbar()

Can anyone tell me why i am getting this error

Thanks in advance.

Kulvinder Singh




Re: Visual Studio Tools for Office Outlook 2007 Toolbar VSTO SE Addin

X4U

Hello Kulvinder,

so I will try to follow:

You loop over the CommandBarControls of the ActiveExplorer (if there is no one )

if you don't find your commandBar you will add a new temporary one (last flag is true)

myCommandBar = this.Application.ActiveExplorer().CommandBars.Add("Test", missing, missing, true);

This means when you release the Outlook Object - your commandBar should be gone.

Why is it still there when you close Outlook and restart

I belive you are missing a GC.Collect or something - or an Outlook instance is still remaining in memory.

Can you check this

Greets, Helmut






Re: Visual Studio Tools for Office Outlook 2007 Toolbar VSTO SE Addin

Kulvinder

Hi Helmut,

Thanks a lot for your reply. I still have two (aleast ) questions left :

1. Is it possible to make sure that the toolbar is loaded everytime in case of Outlook 2003 The code i wrote is for Outlook 2007. I tried running the same code for Outlook 2003 but seems that the ThisAddin_StartUp is not getting fired. Is it possible to bring the toolbar everytime in case of Outlook 2003 as well

2. I corrected the above code by first checking whether my Toolbar is available. If yes, then delete and recreate. That solved the problem. I wanted to save the position of my toolbar as well. So, i corrected the NewExplorer_close event as follows :

Code Snippet

void Explorers_NewExplorer(Microsoft.Office.Interop.Outlook.Explorer Explorer)

{

try

{

LoadToolBar();

((Outlook.ExplorerEvents_10_Event)Explorer).Activate += new Microsoft.Office.Interop.Outlook.ExplorerEvents_10_ActivateEventHandler(ThisAddIn_Activate);

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

}

catch (Exception ex)

{

}

}

void ThisAddIn_Close()

{

try

{

// Toolbar position save code here

}

catch (Exception ex)

{

}

}

I tried to do the same thing in ThisAddin_ShutDown method but that was not getting fired. Any reasons you can tell me I have read somehere that one should release all the Outlook resources otherwise the ShutDown event is not called. So i called ReleaseComObject on myExplorers object in the Close event of the explorer but my toolbar didnt appear when i next closed and restarted Outlook. What i am doing wrong

Thanks for your help in advance.

Regards

Kulvinder Singh






Re: Visual Studio Tools for Office Outlook 2007 Toolbar VSTO SE Addin

Peter Jausovec

Hi,

I am just guessing but if you take a look at the exception the problem seems to be at the line where you set the Visible property. Try commenting out that code and run the addin again.






Re: Visual Studio Tools for Office Outlook 2007 Toolbar VSTO SE Addin

Kulvinder

Hi Helmut,

Can you help

You have been the source of inspiration for doing new things for me.

Thanks in advance.

Kulvinder Singh