I think you can prevent this behavior by checking to see if the menu item exists in the ThisWorkbook_Open event and if it does obtain a reference to the menu rather than creating a new menu each time.
For example, I modified the code in the ThisWorkbook_Open as follows:
ThisWorkbook_Open ' Called when the workbook is opened. Private Sub ThisWorkbook_Open() Handles ThisWorkbook.Open 'Check to see if the menu is present if it is obtain a reference to the MainMenuBar = ThisApplication.CommandBars("Worksheet Menu Bar") MenuBarItem = MainMenuBar.FindControl(, , "&Custom Code") MenuItem = CType(MenuBarItem, Office.CommandBarPopup).Controls(1) Else ' Create menu ' Create menu item control. DirectCast(MenuBarItem, Office.CommandBarPopup), _ "Run Demo Code") End If End Sub
===============================
===============================
'menu and menu bar item. If the menu is not present create it.
If Not IsNothing(ThisApplication.CommandBars("Worksheet Menu Bar").FindControl(, , "&Custom Code")) Then
InitMenuBarItems("&Custom Code")
MenuItem = CreateButton( _
I also modified the code from the url that you referenced (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_vsto2003_ta/html/odc_VSTCommBar.asp ) and added a line of code to set the Tag property of the menu bar to make it easier to search for. I modified the InitMenuBarItems subroutine as follows:
InitMenuBarItems Private Sub InitMenuBarItems(ByVal Caption As String) Try MainMenuBar = ThisApplication.CommandBars( _ "Worksheet Menu Bar") MenuBarItem = MainMenuBar.Controls.Add( _ Office.MsoControlType.msoControlPopup, Temporary:=True) MenuBarItem.Caption = Caption MenuBarItem.Tag = Caption 'Added to make searching easier. Catch ex As Exception MessageBox.Show(ex.Message, _ ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub
==============================
==============================
The remainder of the code I left as is.
Upon testing with these changes I only created one instance of the menu item.
Hope this helps!
Regards,
Ken Laws
MSFT
This posting is provided "AS IS" with no warranties, and confers no rights.
For more information regarding Visual Studio Tools for Office 2005:
Best of Blogs: Visual Studio 2005 Tools for Office
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_2003_ta/html/odc_landvsto2005_ta.asp
Visual Studio Tools for Office Forum
http://forums.microsoft.com/msdn/ShowForum.aspx?ForumID=16
Visual Studio Tools for the Microsoft Office System
http://msdn.microsoft.com/office/understanding/vsto/default.aspx
The code modifications that I provided should have no effect on the manner in which Microsoft Excel opens a file as I see the same behavior that you've described by double-clicking on an Excel file when testing with an Excel file that isn't part of a VSTO solution.
Based on your response it would seem that the best solution would be to use an Excel template with the VSTO project which would allow you to create a new workbook from the template, however, I know Excel Templates were not an option for VSTO 2003, but I wanted to let you know that they will be an option for VSTO 2005.
You can open multiple instances of the file with only one instance of the custom menu bar being displayed, but you would need to open multiple instances of Microsoft Excel and then browse to and open the file in each instance. Please note that opening multiple instances of the workbook from the same location will cause you to receive an Read-only prompt when opening the second instance and any instance thereafter of the workbook.
Regards,
Ken Laws
MSFT
This posting is provided "AS IS" with no warranties, and confers no rights.
For more information regarding Visual Studio Tools for Office 2005:
Best of Blogs: Visual Studio 2005 Tools for Office
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_2003_ta/html/odc_landvsto2005_ta.asp
Visual Studio Tools for Office Forum
http://forums.microsoft.com/msdn/ShowForum.aspx?ForumID=16
Visual Studio Tools for the Microsoft Office System
http://msdn.microsoft.com/office/understanding/vsto/default.aspx