Vsto word2003 add menu bar, add Toolbar

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.