D
Drew Lettington
I have a COM add-in written in C# which creates adds a temporary menu to the
main Word menu bar. I expect the menu to be discarded when Word shuts down
but upon reopening Word, my menu appears (but with menu items from another
add-in's menu).
I currently work around the problem by explicitly deleting my menu in the
OnDisconnection() method and explicitly save the normal template. This seems
like a step that could be avoid if the menu was truely created as temporary.
I suppose by nature of the behavior that the menu is not really being
created as temporary, so I guess my question is - is there something wrong in
the following code such that the menu is not really being created as
temporary?
Here are the new methods that create the menu and its items (the two key
calls are Controls.Add in MakeMenubar and MakeNewMenubarItem where the last
parameter set to true indicates they should be temporary):
private bool MakeMenubar(string menubarName)
{
_menubar = null;
object missing = System.Reflection.Missing.Value;
try
{
// Get the main Word menubar
Microsoft.Office.Core.CommandBar mainMenu =
(Microsoft.Office.Core.CommandBar)_theWordApp.CommandBars.ActiveMenuBar;
// Put new menu at the end
_menubar =
(Microsoft.Office.Core.CommandBarPopup)mainMenu.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlPopup, 1, missing, missing, true);
_menubar.Caption = menubarName;
_menubar.Tag = "MyApp.WordMenubar";
// Add the menu items to the new menu
_menuItem1 = MakeNewMenubarItem(_menubar, "Menu Item 1", 0,
new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(MenuItem1_Click),
"MyApp.WordMenubar.Browse");
_menuItem2 = MakeNewMenubarItem(_menubar, "Menu Item 2", 0,
new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(MenuItem2_Click),
"MyApp.WordMenubar.Refresh");
// More menu items...
return true;
}
catch
{
return false;
}
}
protected Microsoft.Office.Core.CommandBarButton
MakeNewMenubarItem(Microsoft.Office.Core.CommandBarPopup menubar,
string caption, int faceID,
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler clickHandler,
string tag)
{
object missing = System.Reflection.Missing.Value;
try
{
Microsoft.Office.Core.CommandBarButton newButton;
newButton =
(Microsoft.Office.Core.CommandBarButton)menubar.Controls.Add(
Microsoft.Office.Core.MsoControlType.msoControlButton, missing,
missing, missing, true);
newButton.Caption = caption;
newButton.Click += clickHandler;
newButton.Tag = tag;
if (faceID > 0)
newButton.FaceId = faceID;
return newButton;
}
catch
{
return null;
}
}
Any help is greatly appreciated. Thanks.
- Drew
main Word menu bar. I expect the menu to be discarded when Word shuts down
but upon reopening Word, my menu appears (but with menu items from another
add-in's menu).
I currently work around the problem by explicitly deleting my menu in the
OnDisconnection() method and explicitly save the normal template. This seems
like a step that could be avoid if the menu was truely created as temporary.
I suppose by nature of the behavior that the menu is not really being
created as temporary, so I guess my question is - is there something wrong in
the following code such that the menu is not really being created as
temporary?
Here are the new methods that create the menu and its items (the two key
calls are Controls.Add in MakeMenubar and MakeNewMenubarItem where the last
parameter set to true indicates they should be temporary):
private bool MakeMenubar(string menubarName)
{
_menubar = null;
object missing = System.Reflection.Missing.Value;
try
{
// Get the main Word menubar
Microsoft.Office.Core.CommandBar mainMenu =
(Microsoft.Office.Core.CommandBar)_theWordApp.CommandBars.ActiveMenuBar;
// Put new menu at the end
_menubar =
(Microsoft.Office.Core.CommandBarPopup)mainMenu.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlPopup, 1, missing, missing, true);
_menubar.Caption = menubarName;
_menubar.Tag = "MyApp.WordMenubar";
// Add the menu items to the new menu
_menuItem1 = MakeNewMenubarItem(_menubar, "Menu Item 1", 0,
new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(MenuItem1_Click),
"MyApp.WordMenubar.Browse");
_menuItem2 = MakeNewMenubarItem(_menubar, "Menu Item 2", 0,
new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(MenuItem2_Click),
"MyApp.WordMenubar.Refresh");
// More menu items...
return true;
}
catch
{
return false;
}
}
protected Microsoft.Office.Core.CommandBarButton
MakeNewMenubarItem(Microsoft.Office.Core.CommandBarPopup menubar,
string caption, int faceID,
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler clickHandler,
string tag)
{
object missing = System.Reflection.Missing.Value;
try
{
Microsoft.Office.Core.CommandBarButton newButton;
newButton =
(Microsoft.Office.Core.CommandBarButton)menubar.Controls.Add(
Microsoft.Office.Core.MsoControlType.msoControlButton, missing,
missing, missing, true);
newButton.Caption = caption;
newButton.Click += clickHandler;
newButton.Tag = tag;
if (faceID > 0)
newButton.FaceId = faceID;
return newButton;
}
catch
{
return null;
}
}
Any help is greatly appreciated. Thanks.
- Drew