lg4

Hi,

I am trying to add a button to the standard commandbar in Microsoft Word. I am able to create a new commandbar and add a button to the new commandbar but when I get a handle to the standard commandbar and trying and Add a button I get a COM exception "Error HRESULT E_FAIL has been returned from a call to a COM component". The code I am using to add the button is:

Code Block

Microsoft.Office.Core.CommandBar commandbar = ThisDocument.CommandBars["Standard"];

Microsoft.Office.Core.CommandBarButton commandBarButton = (Microsoft.Office.Core.CommandBarButton) commandbar.Controls.Add(1, missing, missing, missing, missing);

Any help would be appreciated.



Re: Visual Studio Tools for Office Adding a control to the standard commandbar in Word ....

Ji Zhou ¨C MSFT

Hi,

I am thinking how you can get these codes compiled. In fact, ThisDocument stands for a class, if we need to use the CommandBars collection. We should use this.CommandBars.(this stands for one instance of ThisDocument Class) The following codes work fine in my side. All other parts of your codes look all right!

Microsoft.Office.Core.CommandBar commandbar = this.CommandBars["Standard"];

Microsoft.Office.Core.CommandBarButton commandBarButton = (Microsoft.Office.Core.CommandBarButton)commandbar.Controls.Add(1, missing, missing, missing, missing);

And you should make sure that the commandbar is not null!

Thanks

Ji






Re: Visual Studio Tools for Office Adding a control to the standard commandbar in Word ....

lg4

Hi Ji,

I made the change in my code from:

Code Block

Microsoft.Office.Core.CommandBar commandbar = ThisDocument.CommadBar.CommandBars["Standard"];

to

Code Block

Microsoft.Office.Core.CommandBar commandbar = this.CommandBars["Standard"];

but I am still getting the same exception. The commandbar object is not either. It seems to return what looks like a proper instantiated commandbar object but still when I try to add a button using the commandbar object it throws the exception .

I am running this code in the Startup function, would this make any difference





Re: Visual Studio Tools for Office Adding a control to the standard commandbar in Word ....

Ji Zhou ¨C MSFT

No, I also put the codes in Startup event handle, but it works OK. I am working on Word 2003.

It seems that the error is related to your machine environment. I searched on the net, and cannot find other people who are stumped in the same problem. Sorry about not able to help anymore, because the issue cannot be reproduced in my side. But I think you can try to repair Office to see if it fixes, for the issue may result from different option settings.

Thanks

Ji






Re: Visual Studio Tools for Office Adding a control to the standard commandbar in Word ....

Cindy Meister

If you still haven't been able to resolve this...

1. I assume this is Word 2003, but if not, please correct me

2. Is this a document-level solution, or an Add-in You don't specify, and I'm guessing document, but for testing it makes sense to be sure one is doing the ssame thing...

3. Are you able to do anything else with the command bar, such as controls.count or controls(1).caption, just to narrow down whether we're looking at a problem with the commandbar, or with the Add method.

3a. If the other properties are working, you might test the Protection and Locked properties, just to make sure the commandbar is actually available to you.

4. CommandBars are actually a member of the APPLICATION object, not the document. I'm guessing the problem might stem from this fact. You might try this.application.commandbars and see if that makes any difference.






Re: Visual Studio Tools for Office Adding a control to the standard commandbar in Word ....

lg4

Hi Cindy,

I have been able to resolve the issue. The problem was the Protection property in the handle to the "Standard" command bar object. It was set to msoBarNoCustomize, when I set it to msoBarNoProtection it added the new button properly.

I answered your question's below anyway in the event it would help someone else with the similar problem. Thanks for the help

1. I assume this is Word 2003, but if not, please correct me.

Yea, is Word 2003. It only needs to run on Word 2003 at the moment and not Word 2007

2. Is this a document-level solution, or an Add-in You don't specify, and I'm guessing document, but for testing it makes sense to be sure one is doing the ssame thing...

It is a document-level solution.

3. Are you able to do anything else with the command bar, such as controls.count or controls(1).caption, just to narrow down whether we're looking at a problem with the commandbar, or with the Add method.

I am able to get a handle to the "Standard" Microsoft.Office.Core.CommandBar object and access the controls in the command bar (i.e. "New Blank Document" button, etc) but when I add a new button to this command bar object, I get the exception. The thing is, if I create a new command bar and try to add a new button to the new command bar, it adds the button properly.

3a. If the other properties are working, you might test the Protection and Locked properties, just to make sure the commandbar is actually available to you.

We set the document to read-only after I try to add the new button to the standard command bar, but the document protection property was set to "Microsoft.Office.Core.MsoBarProtection.msoBarNoCustomize" by default.