Manipulate Microsoft Office Word
Manipulate Microsoft Office Word
Contents
Introduction .................................................................................................................................................. 1
Data Merge Sample ...................................................................................................................................... 1
Create Word Application Object ................................................................................................................... 4
Create Word Application .......................................................................................................................... 4
Create Document Object .......................................................................................................................... 6
Manipulate Word Document – Search and Replace................................................................................... 13
Manipulate Word Document – Modify and Save ....................................................................................... 23
Execute Method .......................................................................................................................................... 36
Test.............................................................................................................................................................. 37
Feedback ..................................................................................................................................................... 39
Introduction
All Microsoft Office applications can be manipulated programmatically using .Net Framework interfaces
provided by Microsoft. The interface is called Primary Interop Assemblies (PIA). This sample
demonstrates merging data into a Word document and save the document to a new file, using PIA.
PIA is very powerful, enabling total control of the Office. But the PIA is not intuitive. This sample project
is trying to point out pitfalls to watch out. The sample uses PIA for Office 2007.
Suppose Microsoft Office is installed and Microsoft Primary Interop Assemblies (PIA) is installed. For
downloading PIA for Office 10, see http://www.microsoft.com/download/en/details.aspx?id=3508 or
search the internet for “PIA downloads” for PIA matching the version for your Office.
We may create a method to do the job of merging the data to the Word file.
In the sample, we first create a Word Application object. The Application object has a Documents
property which has an “Open” method. We use it to create a new Document by opening a Word file.
An Application object appears in the Variable Pane. An action appears in the Action Pane to construct
the Application object:
• All PIA objects to be used must be created via “Add local variable” so that their properties,
methods and events can be accessed for programming
• Only the Application object should be manually constructed
Create Document Object
We must use “Add local variable” to create a Document variable first. Right-click the Method Editor;
choose “Add local variable”:
Select “null” because we should not construct an instance of Document manually. We need to let the
Application object to create Document:
A Document variable appears:
The Word Application has a Documents property. The Documents property has an Open method. We
may use it to open a Word document.
Locate the Documents property of the Word Application and select its Open method:
Choose “Property” for “FileName”:
Select the Text property of the text box for the Word document:
• Constant parameters in a PIA action should be specified via Math Expression so that correct data
types are used.
• Except the Application object, all other PIA objects should not be constructed manually. Some
actions can be used to return PIA objects. Specify “AssignTo” property of such an action to a PIA
variable to get access to a PIA object.
Manipulate Word Document – Search and Replace
Now we have a Document object. We can do data merge into it. We do it by searching and replacing.
Because we are going to do more than one search-replace, we want to create a new method to do it so
that our search-and-replace programming can be re-used many times.
Name it SearchReplace:
We want to pass the Application object to this method. So, add a parameter and name it “app”:
We are asked for the data type for the parameter. Select Word Application from the GAC:
Rename it to “app”:
Add two more parameters: find and replace, both are Strings:
Add “replace”:
Select Execute method of the Find property of the Selection property of the Word application:
For “FindText” of the action, select “Property”:
Select “find” parameter of the method; click “Next”:
Set Value to 1:
Set “Replace” to 2:
Click OK to finish creating this action.
The action appears in the Action Pane. For this method we just need this one action.
Click OK:
It asks us to select the actions to be included in the action-list. We haven’t created the actions to be
included in the action-list. We may create the first action now. Choose “SearchReplace” method; click
“Next”:
For the “app” parameter of the action, select Property:
For the “find” parameter of the action, type the text to be replaced. For this action, type “<name>”:
For parameter “replace”, select “Property” because we want to use value from a text box:
To add more actions into the action-list, right-click the action-list; choose “Edit”:
We can see that the search and replace action we created is in the action list. Click “Copy” button to
create a new Search and Replace action:
Right-click the document object icon; choose “Create action”; choose “More methods”; choose “*All
methods* =>”:
Select Text property of the text box for target file path:
Click OK:
Click OK:
Click OK:
An action is created and assigned to the button:
Test
Click the Run button to compile and run the sample.
Open this document file; we can see that the data from the form are merged into the document:
So, the sample works as we expected.
Feedback
Please send your feedback to [email protected]