Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Outlook / Programming Add-Ins / April 2007

Tip: Looking for answers? Try searching our database.

VSTO Outlook 2003 - Issue with button event handlers

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Soumitra - 20 Apr 2007 00:02 GMT
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
   }

}
Ken Slovak - [MVP - Outlook] - 20 Apr 2007 14:18 GMT
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.

Signature

Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Absolute Beginner's Guide to Microsoft Office Outlook 2003
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm

> Hi,
>
[quoted text clipped - 128 lines]
>
> }
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.