Command Bar Button events not working properly

K

ksskumar

Hi friends,

I am creating Add-ins for outlook. Thru that addin I created Toolbar.
Also I have custom form. When I open the Outlook, addin create the
toolbar. FIRST TIME I can get the message from Button2, Button3,
Button4 and Button5. BUT, after I click the MyButton (It show my
custom form, created in C#), other buttons (Button2, Button3, Button4
and Button5) events are not firing. Only the MyButton code is working.

Here I am attaching my code snippet. (EVEN CODE IS BIT LONG, PLEASE
HELP ME)

If any one know the reason, please let me know.

Thanks in advance
kumar


public string CreateToolBar(Microsoft.Office.Interop.Outlook.Application
Application)
{



Microsoft.Office.Interop.Outlook.Application applicationObject =
new Microsoft.Office.Interop.Outlook.Application();
applicationObject = Application;

Microsoft.Office.Core.CommandBars oCommandBars;
Microsoft.Office.Core.CommandBar oStandardBar;
Microsoft.Office.Core.CommandBarButton MyButton;

Microsoft.Office.Core.CommandBarButton MyButton2;
Microsoft.Office.Core.CommandBarButton MyButton3;
Microsoft.Office.Core.CommandBarButton MyButton4;
Microsoft.Office.Core.CommandBarButton MyButton5;

try
{
oCommandBars = (Microsoft.Office.Core.CommandBars)applicationObject.GetType().InvokeMember("CommandBars",
System.Reflection.BindingFlags.GetProperty , null, applicationObject
,null);
}
catch(Exception)
{
// Outlook has the CommandBars collection on the Explorer object.
object oActiveExplorer;
oActiveExplorer=
applicationObject.GetType().InvokeMember("ActiveExplorer",System.Reflection.BindingFlags.GetProperty,null,applicationObject,null);
oCommandBars= (Microsoft.Office.Core.CommandBars)oActiveExplorer.GetType().InvokeMember("CommandBars",System.Reflection.BindingFlags.GetProperty,null,oActiveExplorer,null);
}

// Set up a custom button on the "Standard" commandbar.
try
{
// oStandardBar = oCommandBars["Standard"];
oStandardBar = oCommandBars.Add("TwigaBar",Microsoft.Office.Core.MsoBarPosition.msoBarTop
,null,false);

}
catch(Exception)
{
// Access names its main toolbar Database.
oStandardBar = oCommandBars["Database"];
}

// In case the button was not deleted, use the exiting one.
try
{
MyButton = (Microsoft.Office.Core.CommandBarButton)oStandardBar.Controls["MyButton"];
MyButton2 = (Microsoft.Office.Core.CommandBarButton)oStandardBar.Controls["MyButton2"];
MyButton3 = (Microsoft.Office.Core.CommandBarButton)oStandardBar.Controls["MyButton3"];
MyButton4 = (Microsoft.Office.Core.CommandBarButton)oStandardBar.Controls["MyButton4"];
MyButton5 = (Microsoft.Office.Core.CommandBarButton)oStandardBar.Controls["MyButton5"];
}

catch(Exception)
{
object omissing = System.Reflection.Missing.Value ;
MyButton = (Microsoft.Office.Core.CommandBarButton)
oStandardBar.Controls.Add(1, omissing , omissing , omissing ,
omissing);
MyButton.Caption = "MyButton";
MyButton.Style =
Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption;
PrepareImage ("1.ico");
MyButton.PasteFace ();

object omissing2 = System.Reflection.Missing.Value ;
MyButton2 = (Microsoft.Office.Core.CommandBarButton)
oStandardBar.Controls.Add(1, omissing2 , omissing2 , omissing2 ,
omissing2);
MyButton2.Caption = "MyButton2";
MyButton2.Style =
Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption;
PrepareImage ("2.ico");
MyButton2.PasteFace ();

object omissing3 = System.Reflection.Missing.Value ;
MyButton3 = (Microsoft.Office.Core.CommandBarButton)
oStandardBar.Controls.Add(1, omissing3 , omissing3 , omissing3 ,
omissing3);
MyButton3.Caption = "MyButton3";
MyButton3.Style =
Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption;
PrepareImage ("3.ico");
MyButton3.PasteFace ();

object omissing4 = System.Reflection.Missing.Value ;
MyButton4 = (Microsoft.Office.Core.CommandBarButton)
oStandardBar.Controls.Add(1, omissing4 , omissing4 , omissing4 ,
omissing4);
MyButton4.Caption = "MyButton4";
MyButton4.Style =
Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption;
PrepareImage ("4.ico");
MyButton4 .PasteFace ();

object omissing5 = System.Reflection.Missing.Value ;
MyButton5 = (Microsoft.Office.Core.CommandBarButton)
oStandardBar.Controls.Add(1, omissing5 , omissing5 , omissing5 ,
omissing5);
MyButton5.Caption = "MyButton5";
MyButton5.Style =
Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption;
PrepareImage ("5.ico");
MyButton5 .PasteFace ();

}

try
{

// The following items are optional, but recommended.
//The Tag property lets you quickly find the control
//and helps MSO keep track of it when more than
//one application window is visible. The property is required
//by some Office applications and should be provided.
MyButton.Tag = "MyButton";
MyButton2.Tag = "MyButton2";
MyButton3.Tag = "MyButton3";
MyButton4.Tag = "MyButton4";
MyButton5.Tag = "MyButton5";

// The OnAction property is optional but recommended.
//It should be set to the ProgID of the add-in, so that if
//the add-in is not loaded when a user presses the button,
//MSO loads the add-in automatically and then raises
//the Click event for the add-in to handle.

MyButton.OnAction = "!<MyCOMAddin.Connect>";
MyButton2.OnAction = "!<MyCOMAddin.Connect>";
MyButton3.OnAction = "!<MyCOMAddin.Connect>";
MyButton4.OnAction = "!<MyCOMAddin.Connect>";
MyButton5.OnAction = "!<MyCOMAddin.Connect>";

MyButton.Visible = true;
MyButton.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.MyButton_Click);
MyButton2.Visible = true;
MyButton2.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.MyButton2_Click);
MyButton3.Visible = true;
MyButton3.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.MyButton3_Click);
MyButton4.Visible = true;
MyButton4.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.MyButton4_Click);
MyButton5.Visible = true;
MyButton5.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.MyButton5_Click);


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

// Display a simple message to show which application you started
in.
//System.Windows.Forms.MessageBox.Show("Addin is loaded",
"MyCOMAddin");
oStandardBar = null;
oCommandBars = null;

return "success";

}
catch(System.Exception e)
{
return e.Message ;
}
}


private void MyButton_Click(Microsoft.Office.Core.CommandBarButton
cmdBarbutton,ref bool cancel)
{
public Form1 frm1 = new Form1();
frm1.Show();
}

private void MyButton2_Click(Microsoft.Office.Core.CommandBarButton
cmdBarbutton,ref bool cancel)
{
System.Windows.Forms.MessageBox.Show("Button 2 clicked","MyAddin");

}

private void MyButton3_Click(Microsoft.Office.Core.CommandBarButton
cmdBarbutton,ref bool cancel)
{
System.Windows.Forms.MessageBox.Show("Button 3 clicked","MyAddin");

}

private void MyButton4_Click(Microsoft.Office.Core.CommandBarButton
cmdBarbutton,ref bool cancel)
{
System.Windows.Forms.MessageBox.Show("Button 4 clicked","MyAddin");
}

private void MyButton5_Click(Microsoft.Office.Core.CommandBarButton
cmdBarbutton,ref bool cancel)
{
System.Windows.Forms.MessageBox.Show("Button 5 clicked","MyAddin");
}
 

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