Why can I not call FindControl on a toolbar object

D

David Thielen

Hi;

The following works:

CommandBars bars =
(CommandBars)ThisApplication.GetType().InvokeMember("CommandBars",
System.Reflection.BindingFlags.GetProperty, null, ThisApplication, null);

CommandBarButton cbc =
(CommandBarButton)bars.FindControl(MsoControlType.msoControlButton,
Type.Missing, caption, false);

But I cannot do the following:


CommandBar toolbar = bars.Add("AutoTag", MsoBarPosition.msoBarTop, false,
true);

for (int ind=0; ind<toolItems.Length; ind++)

InitToolbar(ind, toolItems[ind]);

CommandBarButton cbc =
(CommandBarButton)toolbar.FindControl(MsoControlType.msoControlButton,
Type.Missing, caption, false, true);

Any idea why not?

thanks - dave
 
R

rkozlin

Not sure of how you are setting up the buttons with 'InitToolbar', but I am
using similar code that works perfectly fine. The only difference I can see
is that for the 'FindControl' method I am using true for the 'Visible'
parameter and System.Reflection.Missing.Value for the 'Recursive' parameter.
Perhaps try that?

Also try error wrapping the call to get some information as to what is
failing. Did you debug the code? What line did it fail on etc. Get the
exception information.
 
D

David Thielen

Hi;

I am getting a COM error 0x800A01A8 - so not much info on the exception.

The exception is thrown by the call to:
CommandBarButton cbc =
(CommandBarButton)toolbar.FindControl(MsoControlType.msoControlButton,
Type.Missing, caption, false, true);

Weird - no?
 
R

rkozlin

Is there a specific reason you need to have false,true respectively for the
last two parameters?

That error is a generic error I have received so many times in creating the
add-in I have been working on, so I feel your pain.

Here is a snippet of my code that finds existing buttons on the toolbar I
have created. Looking it over I may have hit the same problem you are now
facing... think that might be why I am first getting the CommandBarControl
object and then casting it. Not sure if this will help you though because
its been quite some time since I wrote this piece. I've been scaling Mount
Everest it seems, ever since.

OCORE.CommandBarControl commC = tlbCommBar.FindControl(1,
System.Reflection.Missing.Value, m_oSettings.ForwardButtonName, true,
System.Reflection.Missing.Value);

if (commC != null)
{
btnDocImport = (OCORE.CommandBarButton)commC;
}
else
{
btnDocImport = (OCORE.CommandBarButton)tlbCommBar.Controls.Add(1,
System.Reflection.Missing.Value, System.Reflection.Missing.Value,
System.Reflection.Missing.Value, false);
}
 
D

David Thielen

Hi;

Well I tried your code and it worked. Then I changed it to my code - and it
worked also. I was testing in a different place in the code.

So I think the answer is that toolbar.FindControl() will work except when a
document has just been closed and the bug in Word has changed the menu COM
objects without updating the menu objects in an AddIn. Of course, that's when
it is needed.

Thanks for the help.
 

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