Document directory
- Reference
- Other resources
This introductory walkthrough describes how to create an application-level external program for Microsoft Office Word. The features you create in this type of solution are available to the application itself, regardless of the opened document.
This walkthrough describes the following tasks:
L create a Word Extension Project for Word 2003 or Word 2007.
L compile the code for adding text to the object model using Word when saving the document.
L generate and run this project to test it.
L clear completed projects so that the programs do not run automatically on the development computer.
Prerequisites
You need the following components to complete this drill:
L Visual Studio Tools for Office (optional components of Visual Studio 2008 Professional Edition and Visual Studio Team System ).
L Word 2003 or Word 2007.
By default, Visual Studio Tools for Office is installed with the listed Visual Studio versions. To check whether it is installed on a computer, see install Visual Studio Tools for Office.
Create a project create a new Word external project in Visual Studio
1. Start Visual Studio.
2. on the "file" menu, point to "new", and then click "project ".
3. In the project type pane, expand Visual C # Or Visual Basic, and then expand Office ".
4. If you want to develop the Word 2007 external program, select the "2007" folder. If you want to develop the Word 2003 external program, select the "2003" folder.
5. In the template pane, select Word 2003 or Word 2007 ".
6. Type FirstWordAddIn in the "name" box.
7. Click OK ".
Visual Studio createsFirstWordAddInProject and open the ThisAddIn code file in the editor.
Write code for adding text to saved documents
Next, add code to the thisaddin code file. The new Code uses the word object model to add sample text to each saved document. By default, the thisaddin code file contains the following generated code:
L Division Definition of the thisaddin class. This class provides code entry points and accesses to the word object model. For more information, see addin host item. The rest of the thisaddin class is defined in a hidden code file. You should not modify this code file.
L thisaddin_startup and thisaddin_shutdown event handlers. These event handlers are called when word loads and unmounts external programs. When loading an external program, use these event handlers to initialize it and use them to clear the resources used by the external program when uninstalling the external program. For more information, see Visual Studio Tools for office project events.
Add text paragraphs to saved documents
In the thisaddin code file, add the following code to the thisaddin class. This new Code defines the event handler for the documentbeforesave event, which is triggered when the document is saved.
When you save a document, the event handler adds the new text to the beginning of the document.
Application_documentbeforesave (byval doc as word. Document, byref saveasui as Boolean ,_
Byref cancel as Boolean) handles application. documentbeforesave
Doc. Paragraphs (1). range. insertparagrapappsfore ()
Doc. Paragraphs (1). range. Text = "this text was added by using code ."
C #
Void Application_DocumentBeforeSave (Word. Document Doc, ref bool SaveAsUI, ref bool Cancel)
{
Doc. Paragraphs [1]. Range. insertparagrapappsfore ();
Doc. Paragraphs [1]. Range. Text = "This text was added by using code .";
}
If you are using C #, add the following required code to the ThisAddIn_Startup event handler. Use this code to connect the Application_DocumentBeforeSave event handler with the DocumentBeforeSave event.
C #
This. Application. DocumentBeforeSave + =
New Word. ApplicationEvents4_DocumentBeforeSaveEventHandler (Application_DocumentBeforeSave );
To save the document and modify it, the previous code example uses the following objects:
L Application field of the ThisAddIn class. The Application field returns a Microsoft. Office. Interop. Word...:. Application object, which indicates the current instance of Word.
L Doc parameter of the event handler for the DocumentBeforeSave event. The Doc parameter is a Microsoft. Office. Interop. Word...:. Document Object that indicates a saved Document. For more information, see the Word object model overview.
Test Item
1. Press F5 to generate and run the project.
When a project is generated, the code is compiled into an assembly, which is included in the generated output folder of the project. Visual Studio also creates a set of registry keys through which Word can discover and load additional programs; visual Studio also configures security settings on the development computer to allow programs to run in addition. For more information, see Office solution generation process overview.
2. in Word, save the activity document.
3. Verify that the following text is added to the document.
This text was added by using code.
4. Disable Word.
Clear Project
After the project is developed, remove the external program assembly, registry key, and security settings from the development computer. Otherwise, each time you open Word on the development computer, the program continues to run.
Clear completed projects on the development computer
L in Visual Studio, click "Clean up Solution" in the "generate" menu ".
Subsequent steps
Now that you have created a basic Word application-level external program, you can learn more about how to develop the external program in the following topics:
L general programming tasks that you can execute in an external program: application-level external program programming.
L programming tasks specific to Word external programs: Word application-level external program development.
L use the Word object model: Word object model overview.
L customize the UI of Word, for example, by adding a custom tab to the functional area or creating your own custom task pane: Office UI customization.
L generate and debug Word application-level external programs: Generate and debug the Office solution.
L deploy Word application-level external applications: deploy the Office solution.
See
Concept
Office solution development overview
Word application-level external program development
Application-level external program Programming
Word object model Overview
Custom Office UI
Overview of Visual Studio Tools for Office project templates
Reference
2003 Microsoft Office external program project template
2007 Microsoft Office external program project template
Other resources
Generate and debug the Office Solution
Deploy Office solutions