Code directly:
Microsoft. Office. Core. commandbar menubar;
Commandbarbutton ccbtn = NULL;
Commandbarbutton btnrequirementproperty;
Commandbarbutton btncancelimport;
Commandbarbutton btncancelimport;
Office. commandbarbutton BTN;
Private void thisaddin_startup (Object sender, system. eventargs E)
{
// This. addmenu ();
// Return;
Word. Application wordapp = This. Application;
Wordapp. Visible = true;
// Microsoft. Office. InterOP. Word. Application application.
// This. Application = This. gethostitem <Microsoft. Office. InterOP. Word. Application> (typeof (Microsoft. Office. InterOP. Word. Application), "application ");
// This. application. activedocument
// Create a command bar
Object missing = system. reflection. Missing. value;
Menubar = (Microsoft. Office. InterOP. Word. Application) wordapp). commandbars. activemenubar; // ["menu bar"];
// Commandbar toolbar = wordapp. commandbars ["menu bar"];
// Delete the menu generated last time.
Microsoft. Office. Core. commandbarcontrols bars = menubar. controls;
Foreach (Microsoft. Office. Core. commandbarcontrol temp_contrl in bars)
{
String T = temp_contrl.tag;
// Delete the object if it already exists.
If (t = "import ")
{
Temp_contrl.delete (false );
}
}
Menubar. Visible = true;
// Add a menu bar
// Level 1 menu
//
Microsoft. Office. Core. commandbarpopup popubar =
(Microsoft. Office. Core. commandbarpopup) menubar. Controls. Add (Microsoft. Office. Core. msocontroltype. msocontrolpopup,
Missing, missing, missing, true );
Popubar. Tag = "import ";
Popubar. Caption = "import ";
Popubar. Visible = true;
// List menu
Btnrequirementproperty =
(Commandbarbutton) popubar. Controls. Add (Microsoft. Office. Core. msocontroltype. msocontrolbutton, 1, "", 1, true );
Btnrequirementproperty. Caption = "single import ";
Btnrequirementproperty. Visible = true;
Btnrequirementproperty. Style = msobuttonstyle. msobuttoniconandcaption;
Btnrequirementproperty. Click + = new Microsoft. Office. Core. _ commandbarbuttonevents_clickeventhandler (this. btn_click );
Btncancelimport =
(Commandbarbutton) popubar. Controls. Add (Microsoft. Office. Core. msocontroltype. msocontrolbutton, 1, "", 1, true );
Btncancelimport. Caption = "batch import ";
Btncancelimport. Visible = true;
Btncancelimport. Style = msobuttonstyle. msobuttoncaption;
Btncancelimport. Click + = new Microsoft. Office. Core. _ commandbarbuttonevents_clickeventhandler (this. btn_click );
// Add a toolbar Tool
Office. commandbar = wordapp. commandbars. Add ("My bar", office. msobarposition. msobartop, false, true );
Commandbar. Visible = true;
/// Add a command bar button
BTN = commandbar. Controls. Add (office. msocontroltype. msocontrolbutton,
Missing, missing, missing, true) as office. commandbarbutton;
BTN. Caption = "My 233332 ";
BTN. Tag = "mybutton ";
BTN. Style = msobuttonstyle. msobuttoncaption;
BTN. Click + = new _ commandbarbuttonevents_clickeventhandler (btn_click );
}
Void btn_click (commandbarbutton Ctrl, ref bool canceldefault)
{
MessageBox. Show ("My button is clicked! "+ Datetime. Now );
}
Problems and Solutions:
1. Each time you run the menu, the menu will be re-added. I will solve this problem here, first Delete the previous menu, and then re-Add the menu
Microsoft. Office. Core. commandbarcontrols bars = menubar. controls;
Foreach (Microsoft. Office. Core. commandbarcontrol temp_contrl in bars)
{
String T = temp_contrl.tag;
// Delete the object if it already exists.
If (t = "import ")
{
Temp_contrl.delete (false );
}
}
2. Add menu and toolbar eventsIt can be executed only once.The second time it didn't respond. Set the button variable to the member variable of the class. It cannot be placed in the method.
Commandbarbutton btnrequirementproperty;
Commandbarbutton btncancelimport;
Office. commandbarbutton BTN;
Vsto word2003 add menu bar, add Toolbar