none
VSTO Outlook code execution issue RRS feed

  • Question

  • I've developed an Outlook Addin that adds a new menu to the Outlook window.  When the user selects an email and then this new menu item, my event handler gets called and does what it is supposed to do.

    Or at least that is what I expect to happen.  What is actually happening, is the application doesn't seem to do anything and doesn't seem to execute the code at all.  When I run this on my Dev machine and debug, I see the same behavior intermittently.  If I add a breakpoint in the event handler, it works every time.  What is causing my addin to selectively execute the event handler for the menu item?

    Here's the code for the event handler:

    		protected void createRequestMenu_Click(Office.CommandBarButton ctrl, ref bool CancelDefault)
    		{
    			ProgressDialog dialog = new ProgressDialog();
    			OutlookUtility outlook = new OutlookUtility();
    			try
    			{
    				Outlook.Selection selectedItems = null;
    				try
    				{
    					selectedItems = thisExplorer.Selection;
    				}
    				catch (Exception exExc)
    				{
    					MessageBox.Show("Please select an email from your Inbox to use this feature. This feature does not work from the Outlook Today screen.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    					return;
    				}
    				dialog.Show();
    				Contact contact = null;
    				string email = outlook.GetSelectedEmail(selectedItems);
    				if (!string.IsNullOrEmpty(email))
    				{
    					contact = Contact.GetContactDetail(email);
    					if (!string.IsNullOrEmpty(contact.ContactID) && !string.IsNullOrEmpty(contact.RelatedSoldToID))
    					{
    						//Save the email text to 
    						dialog.StatusMessage = "Sending Email Text...";
    						Application.DoEvents();
    						string customerEmail = outlook.GetSelectedEmailText(selectedItems);
    						long emailTextID = R2OUtility.SaveEmailText(customerEmail, outlook.GetCreatorEmployeeID());
    						if (emailTextID > 0)
    						{
    							dialog.StatusMessage = "Processing Attachments...";
    							Application.DoEvents();
    							R2OUtility.SaveAttachments(outlook.GetSelectedEmail(selectedItems), emailTextID, outlook.GetAttachments(selectedItems));
    							dialog.StatusMessage = "Launching R2O...";
    							Application.DoEvents();
    							R2OUtility.LaunchR2O(contact.RelatedSoldToID, contact.ContactID, emailTextID);
    							dialog.Hide();
    						}
    						else
    						{
    							dialog.Hide();
    							MessageBox.Show("An error occurred initializing the request. Details have been sent to GIS", "Avnet R2O", MessageBoxButtons.OK, MessageBoxIcon.Error);
    						}
    					}
    					else
    					{
    						dialog.Hide();
    						MessageBox.Show("This contact is not set up in SAP. You must create this contact in SAP before using this feature.", "Avnet R2O", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    					}
    				}
    			}
    			catch (Exception ex)
    			{
    				outlook.HandleExceptionEmail(ex);
    			}
    			finally
    			{
    				dialog.Hide();
    				dialog = null;
    			}
    		}
    

    Thursday, June 9, 2011 9:18 PM

Answers

  • Your CommandBarButton object needs to be declared at a class level scope so it won't get garbage collected while you still need it to fire events.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "RobHarris" <=?utf-8?B?Um9iSGFycmlz?=> wrote in message news:ba5640bb-9c00-435b-aa7f-fb53fe4c9c6c...

    Using Outlook 2007

     

    here's the code for the class constructor and add menu code including the handler wireup:

    		public ExplorerWindow(Outlook.Explorer explorer)
    		{
    			thisExplorer = explorer;
    			AddR2OMenu();
    			Outlook.ExplorerEvents_10_Event explorerEvents = ((Outlook.ExplorerEvents_10_Event)(thisExplorer));
    			explorerEvents.Close += new Outlook.ExplorerEvents_10_CloseEventHandler(Explorers_Close);
    		}
    
    		private void AddR2OMenu()
    		{
    			// Get a reference to the Outlook main menu bar
    			mainMenuBar = thisExplorer.CommandBars.ActiveMenuBar;
    			// Check if there is a Customers menu
    			try
    			{
    				customersMenuIndex = mainMenuBar.Controls["Avnet R2O"].Index;
    			}
    			catch
    			{
    				customersMenuIndex = 0;
    			}
    			if (customersMenuIndex == 0) // Add the menu
    			{
    				// Locate the Help menu
    				try
    				{
    					helpMenuIndex = mainMenuBar.Controls["Help"].Index;
    				}
    				catch
    				{
    					helpMenuIndex = mainMenuBar.Controls.Count;
    				}
    				// Create a menu to the left of the Help menu
    				mainMenu = ((Office.CommandBarPopup)
    				 (mainMenuBar.Controls.Add(
    				 Office.MsoControlType.msoControlPopup,
    				 Type.Missing, Type.Missing, helpMenuIndex, true)));
    				mainMenu.Caption = "Avnet R2O";
    				mainMenu.Tag = MENUTAG;
    				mainMenu.Visible = true;
    				// Create menu item in the menu
    				createRequestMenuItem = ((Office.CommandBarButton)(mainMenu.Controls.Add(Office.MsoControlType.msoControlButton, Type.Missing, Type.Missing, 1, 1)));
    				createRequestMenuItem.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption;
    				createRequestMenuItem.Caption = "Create request from selected";
    				createRequestMenuItem.FaceId = 610;
    				createRequestMenuItem.Tag = "c123";
    				createRequestMenuItem.Click += new Office._CommandBarButtonEvents_ClickEventHandler(createRequestMenu_Click);
    			}
    		}
    
    

    The startup code may also be relevant:

     

    		private void ThisAddIn_Startup(object sender, System.EventArgs e)
    		{
    			explorerWindowList = new List<ExplorerWindow>();
    			explorerWindowList.Add(new ExplorerWindow(this.Application.ActiveExplorer()));
    			explorers = this.Application.Explorers;
    			explorers.NewExplorer += new Outlook.ExplorersEvents_NewExplorerEventHandler(Explorers_NewExplorer);
    		}
    
    


    Ken Slovak MVP - Outlook
    • Marked as answer by Bruce Song Monday, June 20, 2011 11:06 AM
    Friday, June 10, 2011 4:45 PM

All replies

  • You don't show any of the code that actually sets up the event handler, what is that? Are you creating a CommandBarButton object at a class or other scope level so it doesn't go out of scope and get garbage collected? If your createRequestMenu goes out of scope the event handler will never be called.
     
    What version of Outlook is this for?

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "RobHarris" <=?utf-8?B?Um9iSGFycmlz?=> wrote in message news:1bdcc8ce-4937-4c4a-bb8d-6805d41923f4...

    I've developed an Outlook Addin that adds a new menu to the Outlook window.  When the user selects an email and then this new menu item, my event handler gets called and does what it is supposed to do.

    Or at least that is what I expect to happen.  What is actually happening, is the application doesn't seem to do anything and doesn't seem to execute the code at all.  When I run this on my Dev machine and debug, I see the same behavior intermittently.  If I add a breakpoint in the event handler, it works every time.  What is causing my addin to selectively execute the event handler for the menu item?

    Here's the code for the event handler:

    		protected void createRequestMenu_Click(Office.CommandBarButton ctrl, ref bool CancelDefault)
    		{
    			ProgressDialog dialog = new ProgressDialog();
    			OutlookUtility outlook = new OutlookUtility();
    			try
    			{
    				Outlook.Selection selectedItems = null;
    				try
    				{
    					selectedItems = thisExplorer.Selection;
    				}
    				catch (Exception exExc)
    				{
    					MessageBox.Show("Please select an email from your Inbox to use this feature. This feature does not work from the Outlook Today screen.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    					return;
    				}
    				dialog.Show();
    				Contact contact = null;
    				string email = outlook.GetSelectedEmail(selectedItems);
    				if (!string.IsNullOrEmpty(email))
    				{
    					contact = Contact.GetContactDetail(email);
    					if (!string.IsNullOrEmpty(contact.ContactID) && !string.IsNullOrEmpty(contact.RelatedSoldToID))
    					{
    						//Save the email text to 
    						dialog.StatusMessage = "Sending Email Text...";
    						Application.DoEvents();
    						string customerEmail = outlook.GetSelectedEmailText(selectedItems);
    						long emailTextID = R2OUtility.SaveEmailText(customerEmail, outlook.GetCreatorEmployeeID());
    						if (emailTextID > 0)
    						{
    							dialog.StatusMessage = "Processing Attachments...";
    							Application.DoEvents();
    							R2OUtility.SaveAttachments(outlook.GetSelectedEmail(selectedItems), emailTextID, outlook.GetAttachments(selectedItems));
    							dialog.StatusMessage = "Launching R2O...";
    							Application.DoEvents();
    							R2OUtility.LaunchR2O(contact.RelatedSoldToID, contact.ContactID, emailTextID);
    							dialog.Hide();
    						}
    						else
    						{
    							dialog.Hide();
    							MessageBox.Show("An error occurred initializing the request. Details have been sent to GIS", "Avnet R2O", MessageBoxButtons.OK, MessageBoxIcon.Error);
    						}
    					}
    					else
    					{
    						dialog.Hide();
    						MessageBox.Show("This contact is not set up in SAP. You must create this contact in SAP before using this feature.", "Avnet R2O", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    					}
    				}
    			}
    			catch (Exception ex)
    			{
    				outlook.HandleExceptionEmail(ex);
    			}
    			finally
    			{
    				dialog.Hide();
    				dialog = null;
    			}
    		}
    


    Ken Slovak MVP - Outlook
    Friday, June 10, 2011 1:18 PM
  • Using Outlook 2007

     

    here's the code for the class constructor and add menu code including the handler wireup:

    		public ExplorerWindow(Outlook.Explorer explorer)
    		{
    			thisExplorer = explorer;
    			AddR2OMenu();
    			Outlook.ExplorerEvents_10_Event explorerEvents = ((Outlook.ExplorerEvents_10_Event)(thisExplorer));
    			explorerEvents.Close += new Outlook.ExplorerEvents_10_CloseEventHandler(Explorers_Close);
    		}
    
    		private void AddR2OMenu()
    		{
    			// Get a reference to the Outlook main menu bar
    			mainMenuBar = thisExplorer.CommandBars.ActiveMenuBar;
    			// Check if there is a Customers menu
    			try
    			{
    				customersMenuIndex = mainMenuBar.Controls["Avnet R2O"].Index;
    			}
    			catch
    			{
    				customersMenuIndex = 0;
    			}
    			if (customersMenuIndex == 0) // Add the menu
    			{
    				// Locate the Help menu
    				try
    				{
    					helpMenuIndex = mainMenuBar.Controls["Help"].Index;
    				}
    				catch
    				{
    					helpMenuIndex = mainMenuBar.Controls.Count;
    				}
    				// Create a menu to the left of the Help menu
    				mainMenu = ((Office.CommandBarPopup)
    				 (mainMenuBar.Controls.Add(
    				 Office.MsoControlType.msoControlPopup,
    				 Type.Missing, Type.Missing, helpMenuIndex, true)));
    				mainMenu.Caption = "Avnet R2O";
    				mainMenu.Tag = MENUTAG;
    				mainMenu.Visible = true;
    				// Create menu item in the menu
    				createRequestMenuItem = ((Office.CommandBarButton)(mainMenu.Controls.Add(Office.MsoControlType.msoControlButton, Type.Missing, Type.Missing, 1, 1)));
    				createRequestMenuItem.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption;
    				createRequestMenuItem.Caption = "Create request from selected";
    				createRequestMenuItem.FaceId = 610;
    				createRequestMenuItem.Tag = "c123";
    				createRequestMenuItem.Click += new Office._CommandBarButtonEvents_ClickEventHandler(createRequestMenu_Click);
    			}
    		}
    
    

    The startup code may also be relevant:

     

    		private void ThisAddIn_Startup(object sender, System.EventArgs e)
    		{
    			explorerWindowList = new List<ExplorerWindow>();
    			explorerWindowList.Add(new ExplorerWindow(this.Application.ActiveExplorer()));
    			explorers = this.Application.Explorers;
    			explorers.NewExplorer += new Outlook.ExplorersEvents_NewExplorerEventHandler(Explorers_NewExplorer);
    		}
    
    

    Friday, June 10, 2011 3:23 PM
  • Your CommandBarButton object needs to be declared at a class level scope so it won't get garbage collected while you still need it to fire events.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "RobHarris" <=?utf-8?B?Um9iSGFycmlz?=> wrote in message news:ba5640bb-9c00-435b-aa7f-fb53fe4c9c6c...

    Using Outlook 2007

     

    here's the code for the class constructor and add menu code including the handler wireup:

    		public ExplorerWindow(Outlook.Explorer explorer)
    		{
    			thisExplorer = explorer;
    			AddR2OMenu();
    			Outlook.ExplorerEvents_10_Event explorerEvents = ((Outlook.ExplorerEvents_10_Event)(thisExplorer));
    			explorerEvents.Close += new Outlook.ExplorerEvents_10_CloseEventHandler(Explorers_Close);
    		}
    
    		private void AddR2OMenu()
    		{
    			// Get a reference to the Outlook main menu bar
    			mainMenuBar = thisExplorer.CommandBars.ActiveMenuBar;
    			// Check if there is a Customers menu
    			try
    			{
    				customersMenuIndex = mainMenuBar.Controls["Avnet R2O"].Index;
    			}
    			catch
    			{
    				customersMenuIndex = 0;
    			}
    			if (customersMenuIndex == 0) // Add the menu
    			{
    				// Locate the Help menu
    				try
    				{
    					helpMenuIndex = mainMenuBar.Controls["Help"].Index;
    				}
    				catch
    				{
    					helpMenuIndex = mainMenuBar.Controls.Count;
    				}
    				// Create a menu to the left of the Help menu
    				mainMenu = ((Office.CommandBarPopup)
    				 (mainMenuBar.Controls.Add(
    				 Office.MsoControlType.msoControlPopup,
    				 Type.Missing, Type.Missing, helpMenuIndex, true)));
    				mainMenu.Caption = "Avnet R2O";
    				mainMenu.Tag = MENUTAG;
    				mainMenu.Visible = true;
    				// Create menu item in the menu
    				createRequestMenuItem = ((Office.CommandBarButton)(mainMenu.Controls.Add(Office.MsoControlType.msoControlButton, Type.Missing, Type.Missing, 1, 1)));
    				createRequestMenuItem.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption;
    				createRequestMenuItem.Caption = "Create request from selected";
    				createRequestMenuItem.FaceId = 610;
    				createRequestMenuItem.Tag = "c123";
    				createRequestMenuItem.Click += new Office._CommandBarButtonEvents_ClickEventHandler(createRequestMenu_Click);
    			}
    		}
    
    

    The startup code may also be relevant:

     

    		private void ThisAddIn_Startup(object sender, System.EventArgs e)
    		{
    			explorerWindowList = new List<ExplorerWindow>();
    			explorerWindowList.Add(new ExplorerWindow(this.Application.ActiveExplorer()));
    			explorers = this.Application.Explorers;
    			explorers.NewExplorer += new Outlook.ExplorersEvents_NewExplorerEventHandler(Explorers_NewExplorer);
    		}
    
    


    Ken Slovak MVP - Outlook
    • Marked as answer by Bruce Song Monday, June 20, 2011 11:06 AM
    Friday, June 10, 2011 4:45 PM
  • Hi RobHarris,

    What's the status of the problem on your side? Do the suggestions work for you or not? If you still show concern on the post, feel free to follow up and we are glad to help with you.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, June 16, 2011 11:15 AM
  • The problem is intermittent.  Sometimes it works exactly as intended (and always works when a breakpoint is placed in the event handler).  The CommandBarButton is declared at the class level in my ExplorerWindow class.  That class is instantiated in ThisAddin_Startup event so it should not be getting garbage collected and indeed, it doesn't always fail.  

    Also, the alerts for responses don't seem to be working on the site.. I haven't received any alerts that you had responded.

    Here is the complete code for the ExplorerWindow class:

     

    	class ExplorerWindow
    	{
    		private Office.CommandBar mainMenuBar = null;
    		private int helpMenuIndex = 0;
    		private int customersMenuIndex = 0;
    		private Office.CommandBarPopup mainMenu = null;
    		private Office.CommandBarButton createRequestMenuItem = null;
    		private Outlook.Explorer thisExplorer = null;
    		const string MENUTAG = "AvnetR2OAddIn";
    
    		public ExplorerWindow(Outlook.Explorer explorer)
    		{
    			thisExplorer = explorer;
    			AddR2OMenu();
    			Outlook.ExplorerEvents_10_Event explorerEvents = ((Outlook.ExplorerEvents_10_Event)(thisExplorer));
    			explorerEvents.Close += new Outlook.ExplorerEvents_10_CloseEventHandler(Explorers_Close);
    		}
    
    		private void AddR2OMenu()
    		{
    			// Get a reference to the Outlook main menu bar
    			mainMenuBar = thisExplorer.CommandBars.ActiveMenuBar;
    			// Check if there is a Customers menu
    			try
    			{
    				customersMenuIndex = mainMenuBar.Controls["Avnet R2O"].Index;
    			}
    			catch
    			{
    				customersMenuIndex = 0;
    			}
    			if (customersMenuIndex == 0) // Add the menu
    			{
    				// Locate the Help menu
    				try
    				{
    					helpMenuIndex = mainMenuBar.Controls["Help"].Index;
    				}
    				catch
    				{
    					helpMenuIndex = mainMenuBar.Controls.Count;
    				}
    				// Create a menu to the left of the Help menu
    				mainMenu = ((Office.CommandBarPopup)
    				 (mainMenuBar.Controls.Add(
    				 Office.MsoControlType.msoControlPopup,
    				 Type.Missing, Type.Missing, helpMenuIndex, true)));
    				mainMenu.Caption = "Avnet R2O";
    				mainMenu.Tag = MENUTAG;
    				mainMenu.Visible = true;
    				// Create menu item in the menu
    				createRequestMenuItem = ((Office.CommandBarButton)(mainMenu.Controls.Add(Office.MsoControlType.msoControlButton, Type.Missing, Type.Missing, 1, 1)));
    				createRequestMenuItem.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption;
    				createRequestMenuItem.Caption = "Create request from selected";
    				createRequestMenuItem.FaceId = 610;
    				createRequestMenuItem.Tag = "c123";
    				createRequestMenuItem.Click += new Office._CommandBarButtonEvents_ClickEventHandler(createRequestMenu_Click);
    			}
    		}
    		private void RemoveR2OMenu()
    		{
    			Office.CommandBarPopup foundMenu = (Office.CommandBarPopup)thisExplorer.CommandBars.ActiveMenuBar.FindControl(Office.MsoControlType.msoControlPopup, Type.Missing, MENUTAG, true, true);
    			if (foundMenu != null)
    			{
    				foundMenu.Delete(true);
    			}
    		}
    		protected void createRequestMenu_Click(Office.CommandBarButton ctrl, ref bool CancelDefault)
    		{
    			ProgressDialog dialog = new ProgressDialog();
    			OutlookUtility outlook = new OutlookUtility();
    			try
    			{
    				Outlook.Selection selectedItems = null;
    				try
    				{
    					selectedItems = thisExplorer.Selection;
    				}
    				catch (Exception exExc)
    				{
    					MessageBox.Show("Please select an email from your Inbox to use this feature. This feature does not work from the Outlook Today screen.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    					return;
    				}
    				dialog.Show();
    				Application.DoEvents();
    				Contact contact = null;
    				string email = outlook.GetSelectedEmail(selectedItems);
    				if (!string.IsNullOrEmpty(email))
    				{
    					contact = Contact.GetContactDetail(email);
    					if (!string.IsNullOrEmpty(contact.ContactID) && !string.IsNullOrEmpty(contact.RelatedSoldToID))
    					{
    						//Save the email text to 
    						dialog.StatusMessage = "Sending Email Text...";
    						Application.DoEvents();
    						string customerEmail = outlook.GetSelectedEmailText(selectedItems);
    						long emailTextID = R2OUtility.SaveEmailText(customerEmail, outlook.GetCreatorEmployeeID());
    						if (emailTextID > 0)
    						{
    							dialog.StatusMessage = "Processing Attachments...";
    							Application.DoEvents();
    							R2OUtility.SaveAttachments(outlook.GetSelectedEmail(selectedItems), emailTextID, outlook.GetAttachments(selectedItems));
    							dialog.StatusMessage = "Launching R2O...";
    							Application.DoEvents();
    							R2OUtility.LaunchR2O(contact.RelatedSoldToID, contact.ContactID, emailTextID);
    							dialog.Hide();
    						}
    						else
    						{
    							dialog.Hide();
    							MessageBox.Show("An error occurred initializing the request. Details have been sent to GIS", "Avnet R2O", MessageBoxButtons.OK, MessageBoxIcon.Error);
    						}
    					}
    					else
    					{
    						dialog.Hide();
    						MessageBox.Show("This contact is not set up in SAP. You must create this contact in SAP before using this feature.", "Avnet R2O", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    					}
    				}
    			}
    			catch (Exception ex)
    			{
    				outlook.HandleExceptionEmail(ex);
    			}
    			finally
    			{
    				dialog.Hide();
    				dialog = null;
    			}
    		}
    		private void Explorers_Close()
    		{
    			Globals.ThisAddIn.explorerWindowList.Remove(this);
    		}
    
    	}


    Wednesday, June 22, 2011 8:43 PM
  • Alerts have been on and off.
     
    So you have a wrapper class that's instantiated in Startup that is where the event handler is located? What's keeping the wrapper class alive? Do you have a list/collection where it's stored, or is it a static member of the ThisAddin class?

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "RobHarris" <=?utf-8?B?Um9iSGFycmlz?=> wrote in message news:b8a9781a-f75f-4207-8c64-bacfeccdc5eb...

    The problem is intermittent.  Sometimes it works exactly as intended (and always works when a breakpoint is placed in the event handler).  The CommandBarButton is declared at the class level in my ExplorerWindow class.  That class is instantiated in ThisAddin_Startup event so it should not be getting garbage collected and indeed, it doesn't always fail.  

    Also, the alerts for responses don't seem to be working on the site.. I haven't received any alerts that you had responded.


    Ken Slovak MVP - Outlook
    Wednesday, June 22, 2011 9:36 PM
  • Yes, exactly.  It is a List<ExplorerWindow> collection declared in the ThisAddIn class:

    	public partial class ThisAddIn
    	{
    		private Outlook.Explorers explorers = null;
    		internal List<ExplorerWindow> explorerWindowList = null;
    		
    		private void ThisAddIn_Startup(object sender, System.EventArgs e)
    		{
    			explorerWindowList = new List<ExplorerWindow>();
    			explorerWindowList.Add(new ExplorerWindow(this.Application.ActiveExplorer()));
    			explorers = this.Application.Explorers;
    			explorers.NewExplorer += new Outlook.ExplorersEvents_NewExplorerEventHandler(Explorers_NewExplorer);
    		}
    		private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    		{
    
    		}
    		private void Explorers_NewExplorer(Outlook.Explorer explorerWindow)
    		{
    			explorerWindowList.Add(new ExplorerWindow(explorerWindow));
    		}
    	
    		
    		#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
    	}
    

    This way the new Menu shows up on newly spawned Outlook Explorer windows as well as the initial one upon loading.

    Wednesday, June 22, 2011 10:33 PM
  • Does it work better and more consistently if you don't use ActiveExplorer() and substitute Explorers[1] instead when you add the initial Explorer to the list?

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "RobHarris" <=?utf-8?B?Um9iSGFycmlz?=> wrote in message news:16e8e144-3c28-4a8a-964c-71ec56a2736c...

    Yes, exactly.  It is a List<ExplorerWindow> collection declared in the ThisAddIn class:

    	public partial class ThisAddIn
    	{
    		private Outlook.Explorers explorers = null;
    		internal List<ExplorerWindow> explorerWindowList = null;
    		
    		private void ThisAddIn_Startup(object sender, System.EventArgs e)
    		{
    			explorerWindowList = new List<ExplorerWindow>();
    			explorerWindowList.Add(new ExplorerWindow(this.Application.ActiveExplorer()));
    			explorers = this.Application.Explorers;
    			explorers.NewExplorer += new Outlook.ExplorersEvents_NewExplorerEventHandler(Explorers_NewExplorer);
    		}
    		private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    		{
    
    		}
    		private void Explorers_NewExplorer(Outlook.Explorer explorerWindow)
    		{
    			explorerWindowList.Add(new ExplorerWindow(explorerWindow));
    		}
    	
    		
    		#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
    	}
    

    This way the new Menu shows up on newly spawned Outlook Explorer windows as well as the initial one upon loading.


    Ken Slovak MVP - Outlook
    Wednesday, June 22, 2011 11:06 PM
  • That didn't make any difference. But I noticed something else.  I must have found this code where it is using an exception to check whether the menu has already been added so that I don't end up with multiples of the new menu item.  I refactored that section of code and it doesn't seem to be having the issue.  Although, since it is an intermittent problem, it could just appear to be working but basically I changed this:

    			// Check if there is a Customers menu
    			try
    			{
    			  customersMenuIndex = mainMenuBar.Controls["Avnet R2O"].Index;
    			}
    			catch
    			{
    			  customersMenuIndex = 0;
    			}
    


    to this:

     

    			customersMenuIndex = 0;
    			object type = MsoControlType.msoControlPopup;
    			object id = Type.Missing;
    			object tag = MENUTAG;
    			object visible = 1;
    			object recusive = false;
    			//object missing = System.Type.Missing;
    
    			CommandBarPopup barControl = (CommandBarPopup)mainMenuBar.FindControl(type, id, tag, visible, recusive);
    			if (barControl != null)
    			{
    				customersMenuIndex = mainMenuBar.Controls["Avnet R2O"].Index;
    			}
    
    

     

     

    Does it work better and more consistently if you don't use ActiveExplorer() and substitute Explorers[1] instead when you add the initial Explorer to the list?

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "RobHarris" <=?utf-8?B?Um9iSGFycmlz?=> wrote in message news:16e8e144-3c28-4a8a-964c-71ec56a2736c...

    Yes, exactly.  It is a List<ExplorerWindow> collection declared in the ThisAddIn class:

     

    	public partial class ThisAddIn
    	{
    		private Outlook.Explorers explorers = null;
    		internal List<ExplorerWindow> explorerWindowList = null;
    		
    		private void ThisAddIn_Startup(object sender, System.EventArgs e)
    		{
    			explorerWindowList = new List<ExplorerWindow>();
    			explorerWindowList.Add(new ExplorerWindow(this.Application.ActiveExplorer()));
    			explorers = this.Application.Explorers;
    			explorers.NewExplorer += new Outlook.ExplorersEvents_NewExplorerEventHandler(Explorers_NewExplorer);
    		}
    		private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    		{
    
    		}
    		private void Explorers_NewExplorer(Outlook.Explorer explorerWindow)
    		{
    			explorerWindowList.Add(new ExplorerWindow(explorerWindow));
    		}
    	
    		
    		#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
    	}
    

     

    This way the new Menu shows up on newly spawned Outlook Explorer windows as well as the initial one upon loading.


    Ken Slovak MVP - Outlook

    Friday, June 24, 2011 4:13 PM
  • If when you add your controls you set Temporary = true then unless Outlook crashes the controls will never exist when you re-start Outlook.
     
    A construct such as this one:
     
        mainMenuBar.Controls["Avnet R2O"].Index
     
    can cause problems if there is no Avnet R2O control because you are trying to get a property from a null control, which will fire an exception. In general compound dot operators and implied objects are not good in ..NET code. So I would probably change that code if it were in an addin of mine to get the Controls collection first as a CommandBarControls collection object and then I'd try assigning a CommandBarControl object and only if that wasn't null would I try to retrieve the Index property value.
     
    I'm not sure that's relevant to this issue, but it is general good programming practice for working with COM objects in managed code.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "RobHarris" <=?utf-8?B?Um9iSGFycmlz?=> wrote in message news:e8dab2b9-efca-4596-bc3a-6ce5f8c2257b...

    That didn't make any difference. But I noticed something else.  I must have found this code where it is using an exception to check whether the menu has already been added so that I don't end up with multiples of the new menu item.  I refactored that section of code and it doesn't seem to be having the issue.  Although, since it is an intermittent problem, it could just appear to be working but basically I changed this:

    			// Check if there is a Customers menu
    			try
    			{
    			 customersMenuIndex = mainMenuBar.Controls["Avnet R2O"].Index;
    			}
    			catch
    			{
    			 customersMenuIndex = 0;
    			}
    


    to this:

     

    			customersMenuIndex = 0;
    			object type = MsoControlType.msoControlPopup;
    			object id = Type.Missing;
    			object tag = MENUTAG;
    			object visible = 1;
    			object recusive = false;
    			//object missing = System.Type.Missing;
    
    			CommandBarPopup barControl = (CommandBarPopup)mainMenuBar.FindControl(type, id, tag, visible, recusive);
    			if (barControl != null)
    			{
    				customersMenuIndex = mainMenuBar.Controls["Avnet R2O"].Index;
    			}
    
    

     

     

    Does it work better and more consistently if you don't use ActiveExplorer() and substitute Explorers[1] instead when you add the initial Explorer to the list?

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "RobHarris" <=?utf-8?B?Um9iSGFycmlz?=> wrote in message news:16e8e144-3c28-4a8a-964c-71ec56a2736c...

    Yes, exactly.  It is a List<ExplorerWindow> collection declared in the ThisAddIn class:

     

    	public partial class ThisAddIn
    	{
    		private Outlook.Explorers explorers = null;
    		internal List<ExplorerWindow> explorerWindowList = null;
    		
    		private void ThisAddIn_Startup(object sender, System.EventArgs e)
    		{
    			explorerWindowList = new List<ExplorerWindow>();
    			explorerWindowList.Add(new ExplorerWindow(this.Application.ActiveExplorer()));
    			explorers = this.Application.Explorers;
    			explorers.NewExplorer += new Outlook.ExplorersEvents_NewExplorerEventHandler(Explorers_NewExplorer);
    		}
    		private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    		{
    
    		}
    		private void Explorers_NewExplorer(Outlook.Explorer explorerWindow)
    		{
    			explorerWindowList.Add(new ExplorerWindow(explorerWindow));
    		}
    	
    		
    		#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
    	}
    

     

    This way the new Menu shows up on newly spawned Outlook Explorer windows as well as the initial one upon loading.


    Ken Slovak MVP - Outlook


    Ken Slovak MVP - Outlook
    Friday, June 24, 2011 5:28 PM