D
David Thielen
This is what I have found necessary to tie a CommandBarControl to the word
menu item (and I have some questions below):
When your code throws an exception accessing a CommandBar object in your
code, you need to reassign your objects from the Word menu items. Do NOT set
the menu events again as they will then fire twice. The menu events still
work. It's just your COM reference object that is suddenly pointing at
nothing.
Here is the code I came up with to reassign my menu objects (my main menu
item and a single sub menu item):
private CommandBarPopup [] popupMenus = new CommandBarPopup[2];
private CommandBarControl [] buttonMenus = new CommandBarControl[13];
popupMenus[0] = (CommandBarPopup)
ThisApplication.CommandBars.FindControl(MsoControlType.msoControlPopup,
Type.Missing, "WR_MAIN_MENU", false);
buttonMenus[0] =
popupMenus[0].CommandBar.FindControl(MsoControlType.msoControlButton,
Type.Missing, "WR_INSERT_TAG", false, true);
Q1: The explanation I received from another developer (thank you Sergey) is
"I think this issue can appear when Word is recreating its controls. When you
open/close documents you lose references to interfaces of your menu controls.
You need to reconnect them again. I mean you should find your menu controls
in command bar and update variables in the code of your addin. If I get it
right this should help." Is this correct?
Q2: This is not documented anywhere - why not?
Q3: I tried always reapplying the menu items on the
ApplicationEvents3_DocumentChangeEventHandler event. However, when I did this
my menu events would stop. So at present I have to wait unitl a Menu.Enabled
access throws an exception, catch that, then reapply the menu object
assignment. This works but I hate having an exception thrown as part of
normal operation. Is there a way to know when to reapply the menu object
assignment without an exception being thrown?
thanks - dave
menu item (and I have some questions below):
When your code throws an exception accessing a CommandBar object in your
code, you need to reassign your objects from the Word menu items. Do NOT set
the menu events again as they will then fire twice. The menu events still
work. It's just your COM reference object that is suddenly pointing at
nothing.
Here is the code I came up with to reassign my menu objects (my main menu
item and a single sub menu item):
private CommandBarPopup [] popupMenus = new CommandBarPopup[2];
private CommandBarControl [] buttonMenus = new CommandBarControl[13];
popupMenus[0] = (CommandBarPopup)
ThisApplication.CommandBars.FindControl(MsoControlType.msoControlPopup,
Type.Missing, "WR_MAIN_MENU", false);
buttonMenus[0] =
popupMenus[0].CommandBar.FindControl(MsoControlType.msoControlButton,
Type.Missing, "WR_INSERT_TAG", false, true);
Q1: The explanation I received from another developer (thank you Sergey) is
"I think this issue can appear when Word is recreating its controls. When you
open/close documents you lose references to interfaces of your menu controls.
You need to reconnect them again. I mean you should find your menu controls
in command bar and update variables in the code of your addin. If I get it
right this should help." Is this correct?
Q2: This is not documented anywhere - why not?
Q3: I tried always reapplying the menu items on the
ApplicationEvents3_DocumentChangeEventHandler event. However, when I did this
my menu events would stop. So at present I have to wait unitl a Menu.Enabled
access throws an exception, catch that, then reapply the menu object
assignment. This works but I hate having an exception thrown as part of
normal operation. Is there a way to know when to reapply the menu object
assignment without an exception being thrown?
thanks - dave