Walkthrough: Creating Your First Document-Level Customization For Word
Applies to |
---|
The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office. Project type
Microsoft Office version
For more information, see Features Available by Application and Project Type. |
This introductory walkthrough shows you how to create a document-level customization for Microsoft Office Word. The features that you create in this kind of solution are available only when a specific document is open. You cannot use a document-level customization to make application-wide changes, for example, displaying a new Ribbon tab when any document is open.
This walkthrough illustrates the following tasks:
Creating a Word document project for Word 2003 or Word 2007.
Adding text to the document that is hosted in the Visual Studio designer.
Writing code that uses the object model of Word to add text to the customized document when it is opened.
Building and running the project to test it.
Cleaning up the project to remove unnecessary build files and security settings from 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 Visual Studio Settings.
Prerequisites
You need the following components to complete this walkthrough:
Visual Studio Tools for Office (an optional component of Visual Studio 2008 Professional and Visual Studio Team System).
Word 2003 or Word 2007.
By default, Visual Studio Tools for Office is installed with the listed versions of Visual Studio. To check whether it is installed on your computer, see Installing Visual Studio Tools for Office.
Creating the Project
To create a new Word document project in Visual Studio
Start Visual Studio.
On the File menu, point to New, and then click Project.
In the Project Types pane, expand Visual C# or Visual Basic, and then expand Office.
Select the 2007 folder if you are developing a Word 2007 customization, or select the 2003 folder if you are developing a Word 2003 customization.
Note
If you are developing a document-level customization for a particular version of Word, that version must be installed on your development computer.
In the Templates pane, select Word 2003 Document or Word 2007 Document.
In the Name box, type FirstDocumentCustomization.
Click OK.
The Visual Studio Tools for Office Project Wizard opens.
Select Create a new document, and click OK.
Visual Studio creates the FirstDocumentCustomization project, and adds the FirstDocumentCustomization document and ThisDocument code file to the project. The FirstDocumentCustomization document is opened automatically in the designer.
Closing and Reopening the Document in the Designer
If you deliberately or accidentally close the document in the designer while you are developing your project, you can reopen it.
To close and reopen the document in the designer
Close the document by clicking the Close button (X) for the designer window.
In Solution Explorer, right-click the ThisDocument code file, and click View Designer.
- or -
In Solution Explorer, double-click the ThisDocument code file.
Adding Text to the Document in the Designer
You can design the user interface (UI) of your customization by modifying the document that is open in the designer. For example, you can add text, tables, or Word controls. For more information about how to use the designer, see Office Documents in the Visual Studio Environment Overview.
To add text to your document by using the designer
In the document that is open in the designer, type the following text.
This text was added by using the designer.
Adding Text to the Document Programmatically
Next, add code to the ThisDocument code file. The new code uses the object model of Word to add a second paragraph of text to the document. By default, the ThisDocument code file contains the following generated code:
A partial definition of the ThisDocument class, which represents the programming model of the document and provides access to the object model of Word. For more information, see Document Host Item and Word Object Model Overview. The remainder of the ThisDocument class is defined in a hidden code file that you should not modify.
The ThisDocument_Startup and ThisDocument_Shutdown event handlers. These event handlers are called when the document is opened and closed. Use these event handlers to initialize your customization when the document is opened, and to clean up resources used by your customization when the document is closed. For more information, see Visual Studio Tools for Office Project Events.
To add a second paragraph of text to the document by using code
In Solution Explorer, right-click ThisDocument, and then click View Code.
The code file opens in Visual Studio.
Replace the ThisDocument_Startup event handler with the following code. When the document is opened, this code adds a second paragraph of text to the document.
Private Sub ThisDocument_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup Me.Paragraphs(1).Range.InsertParagraphAfter() Me.Paragraphs(2).Range.Text = "This text was added by using code." End Sub
private void ThisDocument_Startup(object sender, System.EventArgs e) { this.Paragraphs[1].Range.InsertParagraphAfter(); this.Paragraphs[2].Range.Text = "This text was added by using code."; }
Note
This code uses the index value 1 to access the first paragraph in the Paragraphs property. 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 Programming with Visual Basic vs. C# in Office Solutions.
Testing the Project
To test your document
Press F5 to build and run your project.
When you build the project, the code is compiled into an assembly that is associated with the document. Visual Studio puts a copy of the document and the assembly in the build output folder for the project, and it configures the security settings on the development computer to enable the customization to run. For more information, see Office Solution Build Process Overview.
In the document, verify that you see the following text.
This text was added by using the designer.
This text was added by using code.
Close the document.
Cleaning up the Project
When you finish developing a project, you should remove the files in the build output folder and the security settings created by the build process.
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 document-level customization for Word, you can learn more about how to develop customizations from these topics:
General programming tasks that you can perform in document-level customizations: Programming Document-Level Customizations.
Programming tasks that are specific to document-level customizations for Word: Word Document-Level Customization Development.
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 actions pane: Office UI Customization.
Using extended Word objects provided by Visual Studio Tools for Office to perform tasks that are not possible by using the Word object model (for example, hosting managed controls on documents and binding Word controls to data by using the Windows Forms data binding model): Automating Word by Using Extended Objects.
Building and debugging document-level customizations for Word: Building and Debugging Office Solutions.
Deploying document-level customizations for Word: Deploying Office Solutions.
See Also
Concepts
Office Solutions Development Overview
Word Document-Level Customization Development
Programming Document-Level Customizations
Automating Word by Using Extended Objects
Visual Studio Tools for Office Project Templates Overview
Reference
Word Document Project Template
Other Resources
Building and Debugging Office Solutions
Change History
Date |
History |
Reason |
---|---|---|
July 2008 |
Added topic. |
Information enhancement. |