My path to VSTO (ii): Basic knowledge of VSTO programs

Source: Internet
Author: User

Original: My path to VSTO (ii): basic knowledge of VSTO procedures

Before I start, let me first introduce my development environment: VS2010 + Office 2010, which is based on. NET Framework 4.0 and VSTO 4.0. The following example code is based on this, if you are using VS2008, then the problem is not big, basically can be ported past. It is important to note that the dynamic type and optional parameters are added to the. Net Framework 4.0, and that the two new features are widely used in VSTO 4.0, and the details of the new features are not covered, but there are many places Google just a little bit, you need to tweak the code slightly when porting.

Create a VSTO Project

Environment construction

To develop a VSTO project, the environment is relatively simple to build, Microsoft has done a good job. Just install the VS2010 (you need to tick VSTO 4.0) and Office2010. VS2008 with Office2007 can also easily build a development environment.

Create a project

Select Create New Project->c#->office, and then select Word add-in, such as:

You can see that in addition to word add-in, there are two types of projects, Word document and Word template. The difference is that add-in belongs to application level, which is a plugin that will be loaded every time word launches. While document and template belong to document level, This project generates a Word file (the document project generates a. docx file, and the template generates. dotx), and the code you write will only work when you run the particular Word file.

Add a custom Ribbon

Right-click the project, add a new Item->office->ribbon (Visual Designer), such as:

Add a button to the Ribbon and name it hello

Double-click the Hello button, and in the onclick, add the following code:

   1:          privatevoid Bthello_click (object sender, Ribboncontroleventargs e)
   2:          {
   3:              System.Windows.Forms.MessageBox.Show ("Hello world!");
   4:          }

Press F5 to run the current program, VS will help you start a new Word instance automatically, then select the Add-ins tab in Word and click the Hello button. If you see Hello word! well dialog box, that means your first VSTO program is completed successfully.

For one of our VSTO programs, we do some small optimizations. As we can see from the previous picture, our ribbon and other plug-in ribbon are crammed into the add-ins, the tabs. We are now going to separate our ribbon. In VS, open the Ribbon's design interface and select Ribbon tab, for example, change the Controlidtype to custom, then change the ControlID to Myfirstaddin so that our ribbon will have a ribbon tab.

* Here's a little trick, if your client wants you to insert the ribbon you've built into office's own ribbon, we can do that. Set Controlidtype to office and set OfficeId to Tabhome, such as:

Run interface

Other OfficeId can be viewed here: Office Ids,office IDs

Add Task Pane Create task Pane

Task pane is a useful control that can reside in the windows of an Office program, allowing developers to create an interface with their own needs. First we create a user control, and then this user control is embedded in the task pane. And the code we add is mostly added to this user control.

Put a label on the UserControl, we don't write further code here for the time being:

In the ThisAddIn.cs file, make the following modifications so that when the addin is loaded, the task Pane is displayed:

   1:  public    partialclass ThisAddIn
   2:      {
   3:  public        null;
   4:  
   5:          privatevoid thisaddin_startup (object sender, System.EventArgs e)
   6:          {
   7:              new ucfortaskpane ();
   8:  this            "My Task Pane");
   9:              _mycustomtaskpane.width = 200;
  Ten:              true;
One   :          }
  :  
  :          privatevoid thisaddin_shutdown (object sender, System.EventArgs e)
  :          {
  :          }
  :  
  :          #region VSTO generated code
  :          //content is omitted
  :          #endregion
  :      }

Operation Result:

Associating a task pane with the Ribbon

We have completed a task Pane, and then we will use the Ribbon previously created to control the display of task Pane. First to make some changes to the previous ribbon, add two buttons, while we do some beautification of the interface, the specific steps I do not detail here, the interface such as:

Add the onclick event for Open task Pane and Close task Pane, with the following code:

   1:          privatevoid Btnopen_click (object sender, Ribboncontroleventargs e)
   2:          {
   3:              ifnull)
   4:              {
   5:                  true;
   6:              }
   7:          }
   8:  
   9:          privatevoid btnClose_Click (object sender, Ribboncontroleventargs e)
  Ten:          {
One   :              ifnull)
  :              {
  :                  false;
  :              }
  :          }
Run your program and you'll see the results. This paper summarizes

Here, I present the development methods of VSTO, as well as the basic knowledge of the Ribbon and task pane, the sample programs in this article can be downloaded here: Myfirstaddin.zip. In a later article, I'll delve into the specific development examples of Word, Excel, and Outlook, as well as the details of development. See for details, I wrote the preface before, Http://www.cnblogs.com/izualx/archive/2011/04/30/2033289.html.

This article welcome reprint, but please keep the source, if you have any questions, you can contact me [email protected]

My path to VSTO (ii): Basic knowledge of VSTO programs

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.