VSTO 2010 - How to make word add-in

This article describes that how to make a Word addin with the help of Visual Studio Tools for Office technology.

The common way to develop new functionality or custom functionality for Outlook, Word or Excel is to develop COM addins. There are many different technologies for making these types of addins such as Visual Basic 6.0, Delphi, C++ and .Net etc.

If we talk ABOUT .NET Then there are two main ways for developing an addin:

With the help of IDTExtensibility interface and others .

With the help of VSTO (Visual Studio Tools for Office) technology.

VSTO supports both languages VB.Net and C# and Office 2003 and later versions.

This article demonstrates how to make a Word addin for Word 2010 by using VSTO 2010.

Objective:

Showing Welcome message on Word application start up

Start project:

Start vsto project File -- > New project -- > installed Template -- >  Visual C# -- >  office -- > 2010

Select Word 2010 add-in

3.jpg

Visual Studio automatically generates an Addin.vb class (thisaddin.vb in vb.net and thisaddin.cs in C#.). We can see this class in our project. We can use this class to perform various tasks such as customizing the user interface of the Word application, accessing the object model of the Office host application (in this project Microsoft is the host application) etc. This type of addin is also called an "application label addin".

There are two default event handlers in this class one is ThisAddin_Startup and other is ThisAddin_ShutDown.

4.jpg

 

A VSTO addin uses the object model of the host application, which means that if we are working for an Outlook addin in VSTO then the addin will use the Outlook object model or if we are working with a Word addin project then the addin application will use the Word object model so our addin application will use Word Object Model.

 

So right now I am explaining some important  things about the Word object model.
The Word object model consists of classes and interfaces that are provided in the primary interop assembly for Word, and are defined in the Microsoft.Office.Interop.Word namespace.

The following sections briefly describe the top-level objects of the Word object model and how they interact with each other. These objects include the following five:

  •          Application object
  •          Document object
  •          Selection object
  •          Range object
  •         Bookmark object

Application Object


The Application object represents the Word application, and it is the root object of other objects. We can use it's properties and methods to control the Word application.

Document Object


The Document object represents a document and all of it's contents. When you open a document or create a new document, you create a new Microsoft.Office.Interop.Word document object, which is added to the Documents collection of the Application object. The document that has the focus is called the active document. It is represented by the ActiveDocument property of the Application object.

Selection Object


The Selection object represents the current selected area. When you perform an operation in the Word user interface, such as bolding text, you select, or highlight the text and then apply the formatting.


Range Object


The Range object represents a contiguous area in a document, and is defined by a starting character position and an ending character position.

Content Control Objects


A Microsoft.Office.Interop.Word ContentControl provides a way for you to control the input and presentation of text and other types of content in Word documents, such as RichText contentcontrol and PlainText content control. 
 

 

Code:

 

public partial class ThisAddIn

    {

        private void ThisAddIn_Startup(object sender, System.EventArgs e)

        {

            MessageBox.Show("Welcome to My Word Add-in");

        }

 

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)

        {

        }

 

       

    }

 

 

Run Addin:

 

Download attached project and build project .

Note: this project requires Visual Studio 2010 and Word 2010 to run successfully.