Why OnConnection, OnStartupComplete and MyButton_Click executes tw

I

Ilia

Hi all,
I downloaded an example from Microsoft about writing add-ins with C#. When I
try it everything executes twice for some reason. Especially annaying is the
tool bar twice execution.

Here is the code:
public void OnConnection(object application,
Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array
custom)
{
applicationObject = (ApplicationClass)application;
addInInstance = addInInst;
if(connectMode != Extensibility.ext_ConnectMode.ext_cm_Startup)
{OnStartupComplete(ref custom);}
}
public void OnDisconnection(Extensibility.ext_DisconnectMode
disconnectMode, ref System.Array custom)
{
if(disconnectMode !=
Extensibility.ext_DisconnectMode.ext_dm_HostShutdown)
{OnBeginShutdown(ref custom);}
applicationObject = null;
}
public void OnStartupComplete(ref System.Array custom)
{
CommandBars oCommandBars;
CommandBar oStandardBar;
try
{
oCommandBars =
(CommandBars)applicationObject.GetType().InvokeMember("CommandBars",
BindingFlags.GetProperty , null, applicationObject ,null);
}
catch(Exception)
{
object oActiveExplorer;
oActiveExplorer=
applicationObject.GetType().InvokeMember("ActiveExplorer",BindingFlags.GetProperty,null,applicationObject,null);
oCommandBars=
(CommandBars)oActiveExplorer.GetType().InvokeMember("CommandBars",BindingFlags.GetProperty,null,oActiveExplorer,null);}
try
{oStandardBar = oCommandBars["Standard"];}
catch(Exception)
{
// Access names its main toolbar Database.
oStandardBar = oCommandBars["Database"];}
// In case the button was not deleted, use the exiting one.
try
{MyButton = (CommandBarButton)oStandardBar.Controls["My Custom
Button"];}
catch(Exception)
{
object omissing = System.Reflection.Missing.Value ;
MyButton = (CommandBarButton) oStandardBar.Controls.Add(1, omissing
, omissing , omissing , omissing);
MyButton.Caption = "My Custom Button";
MyButton.Style = MsoButtonStyle.msoButtonCaption;}

MyButton.Tag = "My Custom Button";
MyButton.OnAction = "!<MyCOMAddin.Connect>";
MyButton.Visible = true;
MyButton.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.MyButton_Click);

object oName =
applicationObject.GetType().InvokeMember("Name",BindingFlags.GetProperty,null,applicationObject,null);

// Display a simple message to show which application you started in.
System.Windows.Forms.MessageBox.Show("This Addin is loaded by " +
oName.ToString() , "MyCOMAddin");
oStandardBar = null;
oCommandBars = null;
}
private void MyButton_Click(CommandBarButton cmdBarbutton,ref bool
cancel)
{
System.Windows.Forms.MessageBox.Show("MyButton was
Clicked","MyCOMAddin"); }
 
H

Helmut Obertanner

Hello Ilia,

maybe you have 2 Explorers (Application-Windows) open.

--
Freundliche Grüße / with regards

Helmut Obertanner
Technical Consultant

Softwaredevelopment
DATALOG Software AG | Zschokkestr. 36 | D-80687 Munich
web: www.datalog.de


.... and IT works!

Ilia said:
Hi all,
I downloaded an example from Microsoft about writing add-ins with C#. When
I
try it everything executes twice for some reason. Especially annaying is
the
tool bar twice execution.

Here is the code:
public void OnConnection(object application,
Extensibility.ext_ConnectMode connectMode, object addInInst, ref
System.Array
custom)
{
applicationObject = (ApplicationClass)application;
addInInstance = addInInst;
if(connectMode != Extensibility.ext_ConnectMode.ext_cm_Startup)
{OnStartupComplete(ref custom);}
}
public void OnDisconnection(Extensibility.ext_DisconnectMode
disconnectMode, ref System.Array custom)
{
if(disconnectMode !=
Extensibility.ext_DisconnectMode.ext_dm_HostShutdown)
{OnBeginShutdown(ref custom);}
applicationObject = null;
}
public void OnStartupComplete(ref System.Array custom)
{
CommandBars oCommandBars;
CommandBar oStandardBar;
try
{
oCommandBars =
(CommandBars)applicationObject.GetType().InvokeMember("CommandBars",
BindingFlags.GetProperty , null, applicationObject ,null);
}
catch(Exception)
{
object oActiveExplorer;
oActiveExplorer=
applicationObject.GetType().InvokeMember("ActiveExplorer",BindingFlags.GetProperty,null,applicationObject,null);
oCommandBars=
(CommandBars)oActiveExplorer.GetType().InvokeMember("CommandBars",BindingFlags.GetProperty,null,oActiveExplorer,null);}
try
{oStandardBar = oCommandBars["Standard"];}
catch(Exception)
{
// Access names its main toolbar Database.
oStandardBar = oCommandBars["Database"];}
// In case the button was not deleted, use the exiting one.
try
{MyButton = (CommandBarButton)oStandardBar.Controls["My Custom
Button"];}
catch(Exception)
{
object omissing = System.Reflection.Missing.Value ;
MyButton = (CommandBarButton) oStandardBar.Controls.Add(1, omissing
, omissing , omissing , omissing);
MyButton.Caption = "My Custom Button";
MyButton.Style = MsoButtonStyle.msoButtonCaption;}

MyButton.Tag = "My Custom Button";
MyButton.OnAction = "!<MyCOMAddin.Connect>";
MyButton.Visible = true;
MyButton.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.MyButton_Click);

object oName =
applicationObject.GetType().InvokeMember("Name",BindingFlags.GetProperty,null,applicationObject,null);

// Display a simple message to show which application you started in.
System.Windows.Forms.MessageBox.Show("This Addin is loaded by " +
oName.ToString() , "MyCOMAddin");
oStandardBar = null;
oCommandBars = null;
}
private void MyButton_Click(CommandBarButton cmdBarbutton,ref bool
cancel)
{
System.Windows.Forms.MessageBox.Show("MyButton was
Clicked","MyCOMAddin"); }
 

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