Before getting started, let me first introduce my development environment: vs2010 + office 2010, which is based on. NET Framework 4.0 and vsto 4.0. The following exampleCodeAll of them are based on this. If you are using vs2008, there is no problem, and you can basically port it. Note that the. NET Framework 4.0Dynamic typeAndOptional ParameterThese two new features are widely used in vsto 4.0. I will not describe the details of the new features (in fact, there are a lot of introductions, So Google will do it ), you need to adjust the code slightly during the migration.
Create a vsto Project
Environment Construction
To develop a vsto project, the environment is easy to build. Microsoft has done a good job in this aspect. You only need to install vs2010 (you need to check vsto 4.0) and office2010. Vs2008 and office2007 can also easily build a development environment.
Create a project
Choose Create Project-> C #-> office, and then select word 2010 add-in, as shown in:
We can see that in addition to word 2010 add-in, there are two project types: Word 2010 document and word 2010 template. The difference between them is that add-in belongs to application level, which is a plug-in that is loaded every time word is started. Document and template belong to document level. this project will generate a wordfile (documentproject will generate a .docx file, and template will generate. dotx). The code you write will only take effect when running this specific word file.
Add a custom RIBBON
Right-click the project and choose add new item> Office> ribbon (visual designer), for example:
Add a button to ribbon and name it "hello"
Double-click "hello" and add the following code in onclick:
1:Private VoidBthello_click (ObjectSender, ribboncontroleventargs E)
2:{
3:System. Windows. Forms. MessageBox. Show ("Hello world! ");
4:}
Press F5 to run the currentProgram, Vs will help you automatically start a new word instance, select the add-ins tab in word, and click "hello. If you see hello word! The dialog box shows that your first vsto program has been successfully completed.
We have made some minor optimizations for one of our vsto programs. From the previous figure, we can see that ribbon and ribbon of other plug-ins are crowded in add-ins, which is a tabs. Now we need to separate our ribbon. In vs, open the ribbon design interface and select the ribbon tab. For example, change the contro1_type to custom and change the controlid to myfirstaddin, so that our ribbon will exclusively own a ribbon tab.
* Here is a small trick. If your customers want you to insert your own ribbon into the office's own ribbon, we can also implement it. Set controlidtype to office and officeid to tabhome, for example:
Run Interface
Other officeids can be viewed here: Office 2010 IDs, Office 2007 IDS
Add task pane to create task pane
Task pane is a useful control that can reside in the windows of office programs, allowing developers to create interfaces with their own needs. First, create a user control, which will be embedded in the task pane. The code we added is mainly added to the user control.
Put a label on usercontrol. We will not write further code here:
In the thisaddin. CS file, make the following changes so that when Addin is loaded, the task pane is displayed:
1:Public Partial ClassThisaddin
2:{
3:PublicCustomtaskpane _ mycustomtaskpane =Null;
4:
5:Private VoidThisaddin_startup (ObjectSender, system. eventargs E)
6:{
7:Ucfortaskpane taskpane =NewUcfortaskpane ();
8:_ Mycustomtaskpane =This. Customtaskpanes. Add (taskpane,"My task pane");
9:-Mycustomtaskpane. width = 200;
10:_ Mycustomtaskpane. Visible =True;
11:}
12:
13:Private VoidThisaddin_shutdown (ObjectSender, system. eventargs E)
14:{
15:}
16:
17:# RegionVsto generated code
18:// Content omitted
19:# Endregion
20:}
Running result:
Associate task pane with ribbon
We have completed a task pane, and we will use the ribbon created earlier to control the display of the task pane. First, modify the previous ribbon and add two buttons. At the same time, we also beautify the interface. I will not detail the specific steps here. The interface is as follows:
Add an onclick event for "Open task pane" and "Close task pane". The Code is as follows:
1:Private VoidBtnopen_click (ObjectSender, ribboncontroleventargs E)
2:{
3:If(Globals. thisaddin. _ mycustomtaskpane! =Null)
4:{
5:Globals. thisaddin. _ mycustomtaskpane. Visible =True;
6:}
7:}
8:
9:Private VoidBtnclose_click (ObjectSender, ribboncontroleventargs E)
10:{
11:If(Globals. thisaddin. _ mycustomtaskpane! =Null)
12:{
13:Globals. thisaddin. _ mycustomtaskpane. Visible =False;
14:}
15:}
Run your program and you will see the result. Summary
Here, I have introduced the development method of vsto and the basic knowledge of ribbon and task pane. The sample program in this article can be downloaded here: myfirstaddin.zip. AfterArticle. For details, see the preface I wrote earlier, http://www.cnblogs.com/izualx/archive/2011/04/30/2033289.html.
Please repost this article, but please keep the source, if you have any questions, you can contact me justin.tyrael@gmail.com