This article was previously published under Q303294
This article has been archived. It is offered "as is" and will no longer be updated.
This article illustrates how to automate Microsoft Word with Microsoft Visual Basic .NET to retrieve and to manipulate document properties. Although the sample in this article is specifically written to automate Word, the same concepts can be applied to Microsoft Excel and Microsoft PowerPoint.
Create an Automation client for Microsoft Word
Start Visual Studio .NET.
On the File menu, click New, and then click Project. Select Windows Application from the Visual Basic Project types. Form1 is created by default.
Add a reference to Microsoft Word Object Library. To do this, follow these steps:
On the Project menu, click Add Reference.
On the COM tab, locate the Microsoft Word Object Library, and then click Select.
Note Microsoft Office 2003 includes Primary Interop Assemblies (PIAs). Microsoft Office XP does not include PIAs, but they can be downloaded. For more information about Office XP PIAs, click the following article number to view the article in the Microsoft Knowledge Base:
328912 Microsoft Office XP primary interop assemblies (PIAs) are available for download
Click OK in the Add References dialog box to accept your selections. If you are prompted to generate wrappers for the libraries that you selected, click Yes.
On the View menu, select Toolbox to display the toolbox, and add a button to Form1.
Double-click Button1. The code window for the form appears.
In the code window, replace the following code
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles Button1.ClickEnd Sub
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles Button1.Click Dim oWord As Word.Application Dim oDoc As Word.Document Dim oBuiltInProps As Object Dim oCustomProps As Object Dim oProp As Object Dim strValue As String 'Create an instance of Word and make it visible. oWord = CreateObject("Word.Application") oWord.Visible = True 'Create a new document oDoc = oWord.Documents.Add() 'Get the Built-in Document Properties collection. oBuiltInProps = oDoc.BuiltInDocumentProperties 'Get the value of the Author property and display it strValue = oBuiltInProps.Item("Author").Value MsgBox("The author of this document is " & strValue) 'Set the value of the Subject property. oBuiltInProps.Item("Subject").Value = _ "Knowledge Base article Q303294" 'Get the Custom Document Properties collection. oCustomProps = oDoc.CustomDocumentProperties 'Add a property named Knowledge Base Article 'and give it a value of Q303294. oCustomProps.Add("Knowledge Base article", False, _ Office.MsoDocProperties.msoPropertyTypeString, "Q303294") 'Display a message box to give the user a chance to verify the 'properties. MsgBox("Select Properties from the File menu " _ & "to view the changes." & Chr(10) _ & "Select the Summary tab to view " _ & "the Subject and the Custom tab to view the Custom " _ & "properties.", MsgBoxStyle.Information, _ "Check File Properties") 'Clean up. We'll leave Word running. oCustomProps = Nothing oBuiltInProps = Nothing oDoc = Nothing oWord = Nothing End Sub
Add the following code to the top of Form1.vb:
Imports Office = Microsoft.Office.CoreImports Word = Microsoft.Office.Interop.Word
Press F5 to run the application.
Click Button1 to start Microsoft Word.
This code demonstrates reading and writing both the built-in document properties and the custom document properties. When run, this code displays the value of the built-in Author property, changes the Subject property value to "Knowledge Base article Q303294," and creates a new custom document property that is named "Knowledge Base article." When you are prompted to view the changes, switch to Word, and click Properties on the File menu.
For more information, visit the following Microsoft Developer Network Web site:
Microsoft Visual Basic .NET 2003 Standard Edition, Microsoft Visual Basic .NET 2002 Standard Edition, Microsoft Office Excel 2003, Microsoft Excel 2002 Standard Edition, Microsoft Office PowerPoint 2003, Microsoft PowerPoint 2002 Standard Edition, Microsoft Office Word 2003, Microsoft Word 2002