Why is a temporary menu item stored in normal.dot?

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
 
T

TF

Menu Bar (and many other customisation changes) are saved to normal.dot on
closing Word. The setting 'Prompt to Save Normal Template' is usually
checked by default, so when Word is closed, you should have the option to
say NO to saving the changes. That's the way Word works. If you want to find
a better way of doing this, I suggest that you post this query in the
appropriate Word.VBA newsgroup rather than in application.errors as your
question doesn't really fit here.



message :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
 
D

Drew Lettington

My understanding was that by creating a menu or toolbar as temporary, it was
explictly *not* supposed to be saved in the document template.

- Drew
 
T

TF

Drew

That may be true as I don't have the VBA knowledge to say otherwise. I
recommend the VBA newsgroups because that is where the real VBA gurus
hangout - although they do occasionally look through here too.

Terry

message : My understanding was that by creating a menu or toolbar as temporary, it
was
: explictly *not* supposed to be saved in the document template.
:
: - Drew
:
: "TF" wrote:
:
: > Menu Bar (and many other customisation changes) are saved to normal.dot
on
: > closing Word. The setting 'Prompt to Save Normal Template' is usually
: > checked by default, so when Word is closed, you should have the option
to
: > say NO to saving the changes. That's the way Word works. If you want to
find
: > a better way of doing this, I suggest that you post this query in the
: > appropriate Word.VBA newsgroup rather than in application.errors as your
: > question doesn't really fit here.
: >
: > --
: > Terry Farrell - Word MVP
:
: >
: > message : > :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
: >
: >
: >
 

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