Walkthrough: Create your first VSTO Add-in for Word
Applies to: Visual Studio Visual Studio for Mac
Note
This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here
This introductory walkthrough shows you how to create a VSTO Add-in for Microsoft Office Word. The features that you create in this kind of solution are available to the application itself, regardless of which documents are open.
Applies to: The information in this topic applies to VSTO Add-in projects for Word. For more information, see Features available by Office application and project type.
This walkthrough illustrates the following tasks:
Creating a Word VSTO Add-in project.
Writing code that uses the object model of Word to add text to a document when it is saved.
Building and running the project to test it.
Cleaning up the completed project so that the VSTO Add-in no longer runs automatically on your development computer.
Note
Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Personalize the IDE.
Prerequisites
You need the following components to complete this walkthrough:
An edition of Visual Studio that includes the Microsoft Office developer tools. For more information, see Configure a computer to develop Office solutions.
Microsoft Word
Create the project
To create a new Word VSTO Add-in project in Visual Studio
Start Visual Studio.
On the File menu, point to New, and then click Project.
In the templates pane, expand Visual C# or Visual Basic, and then expand Office/SharePoint.
Under the expanded Office/SharePoint node, select the Office Add-ins node.
In the list of project templates, select a Word VSTO Add-in project.
In the Name box, type FirstWordAddIn.
Click OK.
Visual Studio creates the FirstWordAddIn project and opens the ThisAddIn code file in the editor.
Write code to add text to the saved document
Next, add code to the ThisAddIn code file. The new code uses the object model of Word to add boilerplate text to each saved document. By default, the ThisAddIn code file contains the following generated code:
A partial definition of the
ThisAddIn
class. This class provides an entry point for your code and provides access to the object model of Word. For more information, see Program VSTO Add-ins. The remainder of theThisAddIn
class is defined in a hidden code file that you should not modify.The
ThisAddIn_Startup
andThisAddIn_Shutdown
event handlers. These event handlers are called when Word loads and unloads your VSTO Add-in. Use these event handlers to initialize your VSTO Add-in when it is loaded, and to clean up resources used by your VSTO Add-in when it is unloaded. For more information, see Events in Office projects.
To add a paragraph of text to the saved document
In the ThisAddIn code file, add the following code to the
ThisAddIn
class. The new code defines an event handler for the DocumentBeforeSave event, which is raised when a document is saved.When the user saves a document, the event handler adds new text at the start of the document.
Private Sub Application_DocumentBeforeSave(ByVal Doc As Word.Document, ByRef SaveAsUI As Boolean, _ ByRef Cancel As Boolean) Handles Application.DocumentBeforeSave Doc.Paragraphs(1).Range.InsertParagraphBefore() Doc.Paragraphs(1).Range.Text = "This text was added by using code." End Sub
void Application_DocumentBeforeSave(Word.Document Doc, ref bool SaveAsUI, ref bool Cancel) { Doc.Paragraphs[1].Range.InsertParagraphBefore(); Doc.Paragraphs[1].Range.Text = "This text was added by using code."; }
Note
This code uses an index value of 1 to access the first paragraph in the Paragraphs collection. Although Visual Basic and Visual C# use 0-based arrays, the lower array bounds of most collections in the Word object model is 1. For more information, see Write code in Office solutions.
If you are using C#, add the following required code to the
ThisAddIn_Startup
event handler. This code is used to connect theApplication_DocumentBeforeSave
event handler with the DocumentBeforeSave event.this.Application.DocumentBeforeSave += new Word.ApplicationEvents4_DocumentBeforeSaveEventHandler(Application_DocumentBeforeSave);
To modify the document when it is saved, the previous code examples use the following objects:
The
Application
field of theThisAddIn
class. TheApplication
field returns a Application object, which represents the current instance of Word.The
Doc
parameter of the event handler for the DocumentBeforeSave event. TheDoc
parameter is a Document object, which represents the saved document. For more information, see Word object model overview.
Test the project
To test the project
Press F5 to build and run your project.
When you build the project, the code is compiled into an assembly that is included in the build output folder for the project. Visual Studio also creates a set of registry entries that enable Word to discover and load the VSTO Add-in, and it configures the security settings on the development computer to enable the VSTO Add-in to run. For more information, see Build Office solutions.
In Word, save the active document.
Verify that the following text is added to the document.
This text was added by using code.
Close Word.
Clean up the project
When you finish developing a project, remove the VSTO Add-in assembly, registry entries, and security settings from your development computer. Otherwise, the VSTO Add-in will continue to run every time that you open Word on your development computer.
To clean up the completed project on your development computer
- In Visual Studio, on the Build menu, click Clean Solution.
Next steps
Now that you have created a basic VSTO Add-in for Word, you can learn more about how to develop VSTO Add-ins from these topics:
General programming tasks that you can perform in VSTO Add-ins: Program VSTO Add-ins.
Programming tasks that are specific to Word VSTO Add-ins: Word solutions.
Using the object model of Word: Word object model overview.
Customizing the UI of Word, for example, by adding a custom tab to the Ribbon or creating your own custom task pane: Office UI customization.
Building and debugging VSTO Add-ins for Word: Build Office solutions.
Deploying VSTO Add-ins for Word: Deploy an Office solution.