Bug Report - CommandBarControl.Enabled throws an exception

D

David Thielen

Hi;

I am trying somehow to get these bugs reported to the Word group
(having no luck so far):

CommandBarControl.Enabled = true/false throws an exception. Here is
how it happens. If I start Word, type something in the new default
document, open a second document, close the first instance of Word
(document and application), then close the document in the second
instance of Word leaving Word with no document, then when the
ThisApplication.WindowSelectionChange event is fired, I try to disable
all menu items based on caret position (no caret == no caret
position). It throws an exception at this point. I can still run the
menu events, I just can’t enable/disable the menu itself. If I then
open a document in Word - sometimes it will then let me enable/disable
the menu items. But usually not. This happens on my computer, but not
on my daughter’s computer.

However, even after this happens, the menu events still fire when the
user selects that menu item.

thanks - dave
 
S

Sergey (www.add-in-express.com)

Hi, David.
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.

Good luck.
 
D

David Thielen

THANK YOU. This was it. (I even had some developers in the Word group
who looked at this issue and said it had to be a bug in my code.)

As this is not doucmented anywhere, here is what you need to do. 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 saty. It's just your COM reference object that is suddenly
pointing at nothing.

Here is the code I came up with (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);

Good luck all

- dave
 

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