VSTO Outlook 2003 - Issue with button event handlers

S

Soumitra

Hi,

I am developing a VSTO add-in for Outlook 2003 using VS. NET 2005 VSTO
template. I am adding two command buttons to Outlook's standard tool
bar. These buttons are hooked up to an event handler
(Button1_Click). When I start outlook I see the two buttons, however
for some reason only one button is hooked to the event handler. This
does not depend on the order in which the buttons are created. There
seems to be no patter to this. Sometimes event handler for both
buttons work. But usually the event handler for the button that is
clicked first works. To me it seems like Outlook disposes the second
button for some reason. I have pasted the code inside ThisAddIn.cs
below. In this code the two buttons are added to two separate Popup.
However, the result is same as directly adding the buttons to the
standard tool bar.

Please let me know if anyone has any thoughts on this.

Thanks,
Soumitra Mishra

namespace IssueEmailAddIn2003
{
public partial class ThisAddIn
{
private Microsoft.Office.Interop.Outlook.Selection
currentSelection;
public Microsoft.Office.Interop.Outlook.Selection
CurrentSelection
{
get { return
this.Application.ActiveExplorer().Selection; }
}
// Called only the first time
private void ThisAddIn_Startup(object sender, System.EventArgs
e)
{
try
{
// Add VSTO Menus
CreateVSTOPopupMenus();
}
catch (Exception exp)
{
MessageBox.Show("Error: " + exp.ToString());
}
}
public void CreateVSTOPopupMenus()
{
Office.CommandBar currentBar =
this.Application.ActiveExplorer().CommandBars["Standard"];

// Add Popup Menus
CreateEmailIssuePopUp(); // Popup 1
CreateFavoritesMenu(); // Popup 2

}
private void CreateEmailIssuePopUp()
{
Office.CommandBar currentBar =
this.Application.ActiveExplorer().CommandBars["Standard"];

Office.CommandBarPopup c =
(Office.CommandBarPopup)currentBar.Controls.Add(Office.MsoControlType.msoControlPopup,
1, "", 1, true);
c.Caption = "TAC Net";
c.Visible = true;
c.Tag = "TAC Net";

PopulateEmailIssueListPopUp(c);
}
public void CreateFavoritesMenu()
{
// Get a reference to outlook's command bar
Office.CommandBar currentBar =
this.Application.ActiveExplorer().CommandBars["Standard"];
Office.CommandBarPopup favoritesMenu =
(Office.CommandBarPopup)currentBar.Controls.Add(Office.MsoControlType.msoControlPopup,
1, "", 1, true);
favoritesMenu.Caption = "Favorites";
favoritesMenu.Visible = true;
favoritesMenu.Tag = "Favorites";
PopulateEmailIssueListPopUp(favoritesMenu);
}
private void
PopulateEmailIssueListPopUp(Office.CommandBarPopup
emailIssueListPopUp)
{
Office.CommandBarControl button =
emailIssueListPopUp.Controls.Add(Office.MsoControlType.msoControlButton,
System.Type.Missing, "dummy", missing, false);
button.Caption = "* dummy";
// Add event handler to save the email to the
corresponding document library
Office.CommandBarButton buttonControl = button as
Office.CommandBarButton;
//buttonControl.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(EmailIssueButton_Click);
buttonControl.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(Button1_Click);
}
private void
Button1_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool
CancelDefault)
{
MessageBox.Show(Ctrl.Caption + ":" +
CancelDefault.ToString());
}
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
}

}
 
K

Ken Slovak - [MVP - Outlook]

Anything you want kept alive has to be declared at the module level of the
class, not locally in some procedure. The garbage collector is eating your
button references. Declare your button objects at module level.




Soumitra said:
Hi,

I am developing a VSTO add-in for Outlook 2003 using VS. NET 2005 VSTO
template. I am adding two command buttons to Outlook's standard tool
bar. These buttons are hooked up to an event handler
(Button1_Click). When I start outlook I see the two buttons, however
for some reason only one button is hooked to the event handler. This
does not depend on the order in which the buttons are created. There
seems to be no patter to this. Sometimes event handler for both
buttons work. But usually the event handler for the button that is
clicked first works. To me it seems like Outlook disposes the second
button for some reason. I have pasted the code inside ThisAddIn.cs
below. In this code the two buttons are added to two separate Popup.
However, the result is same as directly adding the buttons to the
standard tool bar.

Please let me know if anyone has any thoughts on this.

Thanks,
Soumitra Mishra

namespace IssueEmailAddIn2003
{
public partial class ThisAddIn
{
private Microsoft.Office.Interop.Outlook.Selection
currentSelection;
public Microsoft.Office.Interop.Outlook.Selection
CurrentSelection
{
get { return
this.Application.ActiveExplorer().Selection; }
}
// Called only the first time
private void ThisAddIn_Startup(object sender, System.EventArgs
e)
{
try
{
// Add VSTO Menus
CreateVSTOPopupMenus();
}
catch (Exception exp)
{
MessageBox.Show("Error: " + exp.ToString());
}
}
public void CreateVSTOPopupMenus()
{
Office.CommandBar currentBar =
this.Application.ActiveExplorer().CommandBars["Standard"];

// Add Popup Menus
CreateEmailIssuePopUp(); // Popup 1
CreateFavoritesMenu(); // Popup 2

}
private void CreateEmailIssuePopUp()
{
Office.CommandBar currentBar =
this.Application.ActiveExplorer().CommandBars["Standard"];

Office.CommandBarPopup c =
(Office.CommandBarPopup)currentBar.Controls.Add(Office.MsoControlType.msoControlPopup,
1, "", 1, true);
c.Caption = "TAC Net";
c.Visible = true;
c.Tag = "TAC Net";

PopulateEmailIssueListPopUp(c);
}
public void CreateFavoritesMenu()
{
// Get a reference to outlook's command bar
Office.CommandBar currentBar =
this.Application.ActiveExplorer().CommandBars["Standard"];
Office.CommandBarPopup favoritesMenu =
(Office.CommandBarPopup)currentBar.Controls.Add(Office.MsoControlType.msoControlPopup,
1, "", 1, true);
favoritesMenu.Caption = "Favorites";
favoritesMenu.Visible = true;
favoritesMenu.Tag = "Favorites";
PopulateEmailIssueListPopUp(favoritesMenu);
}
private void
PopulateEmailIssueListPopUp(Office.CommandBarPopup
emailIssueListPopUp)
{
Office.CommandBarControl button =
emailIssueListPopUp.Controls.Add(Office.MsoControlType.msoControlButton,
System.Type.Missing, "dummy", missing, false);
button.Caption = "* dummy";
// Add event handler to save the email to the
corresponding document library
Office.CommandBarButton buttonControl = button as
Office.CommandBarButton;
//buttonControl.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(EmailIssueButton_Click);
buttonControl.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(Button1_Click);
}
private void
Button1_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool
CancelDefault)
{
MessageBox.Show(Ctrl.Caption + ":" +
CancelDefault.ToString());
}
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
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top