CommandBarControl - keeping them tied to the menu item

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
 
P

Peter Huang

Hi

1. Can you provide a simple reproduce sample together with detailed
reproduce steps.?
2. We have reported similar issue to the dev team. Here is a scenario which
we need to rebind the event.
http://groups.google.com/groups?hl=zh-CN&lr=&c2coff=1&threadm=H9c3msktEHA.31
52%40cpmsftngxa10.phx.gbl&rnum=13&prev=/groups%3Fq%3D%2522peter%2Bhuang%2522
%2Bcombobox%26hl%3Dzh-CN%26lr%3D%26c2coff%3D1%26start%3D10%26sa%3DN

3. I think you may try to do the job in the application's WindowActivate
event to see if that works.

If you still have any concern, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

David Thielen

A simple sample - no.

However, I am willing to give the Word team the complete set of code to my
add-in as long as they keep it confidential. And the steps to make this occur
are very simple using that code. If you want that, please email me at
(e-mail address removed).

I tried the Activate event - same thing - it loses the events. Very weird.

thanks - dave
 
D

David Thielen

This should be a known bug. Can't I just get what the solution or work-around
is? And if I contact MSPSS then I am left having to pay Microsoft to report a
bug - which I think is getting the situation backwards.

??? - dave
 
P

Peter Huang

Hi

I am sorry if I did not make myself more clearly.
From your description, it seems that your problem is that you can not get
access to the CommandBar object sometimes.
If I have any misunderstanding, please feel free to post here.
For this problem we have reported to our dev team, I can not guarantee when
the fix for this problem will be provides. Why I suggest contact MS PSS is
because that in newsgroup support we did not provide hotfix to customer, so
if you really eager to fix the problem, you need to contact MS PSS to
request for a hotfix.

Hope this helps.
If you still have any concern, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

David Thielen

I understand that the bug won't be fixed for awhile. What I was hoping was
that you could tell us what the work around is. Specifically on what events
the menu objects should be reapplied to the Word menu without having to catch
an exception.

I would guess once they understand the bug, the developers could tell you so
you could tell us how to handle this. (I would also guess a bug that is this
easy to hit has been known to them for some time and therefore they already
know the work-around.)

thanks - dave
 
P

Peter Huang

Hi

Based on my research, so far the only workaround is to enbrace the code
that access to the commandbars in a try catch block.
Thanks for your understanding!
If you still have any concern please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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