Visio CommandBar click handler - COM exception "HRESULT 0x800A01A8

P

Petrowi

I have this code attach a command bar in Visio 2003 and add a button to it.

The code works ok until a document is opened. The error looks like garbage
collection issue, but I keep a reference to the .Net object, so it shouldn't
get collected.

For some reason after a document is opened, access to cbbX throws a COM
exception "HRESULT 0x800A01A8"

I can't figure out what is going wrong, please help.
Thank you.


public static Core.CommandBarButton cbbX = null;

public static Core.CommandBar cb = null;

public static void AddToolbar(Visio.Application va) {
Core.CommandBars cbrs = va.CommandBars as Core.CommandBars;
if (cbrs == null) {
System.Windows.Forms.MessageBox.Show("Visioplugin can't
create Toolbar [MNADCBRC]");
} else {

cb = cbrs.Add("X Toolbar", Core.MsoBarPosition.msoBarTop,
Type.Missing, true);
cb.Visible = true;
object o = Type.Missing;
Core.CommandBarButton testButton =
(Core.CommandBarButton)cb.Controls.Add(Core.MsoControlType.msoControlButton,
o, o, o, true);
testButton.Caption = "X Button";
testButton.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(testButton_Click);
testButton.Style = Core.MsoButtonStyle.msoButtonCaption;
testButton.Tag = "X Button";
cbbXDLS = testButton;

}
}

static void testButton_Click(Microsoft.Office.Core.CommandBarButton
Ctrl, ref bool CancelDefault) {

if (Ctrl.Tag == cbbX.Tag) // THIS LINE THROWS "HRESULT 0x800A01A8"

System.Windows.Forms.MessageBox.Show("Visio Click!");
}
 
N

nbelyh

I have this code attach a command bar in Visio 2003 and add a button to it.

The code works ok until a document is opened. The error looks like garbage
collection issue, but I keep a reference to the .Net object, so it shouldn't
get collected.

When Visio opens a document, it "re-builds" the toolbars. That is,
probably you have another set of toolbars. Try closing the document and
clicking the button. Does this work?
Maybe the document you are trying to open has "custom toolbars"
defined. Have you tried this with an empty document?
Probably, you'd want to re-integrate your toolbar at the moment when
document gets opened.
Or use so-called "UIObjects" API instead of "CommandBars" API.
NB: You will probably experience just the same problem if you'll try to
customize Visio's toolbars with "Customize" command.
 

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