Microsoft KB Archive/303294

= How to use Automation to get and to set Office Document properties with Visual Basic .NET =

Article ID: 303294

Article Last Modified on 6/29/2007

-

APPLIES TO


 * 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 Standard Edition

-



This article was previously published under Q303294



SUMMARY
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.</ol> </li> On the View menu, select Toolbox to display the toolbox, and add a button to Form1.</li> Double-click Button1. The code window for the form appears.</li>  In the code window, replace the following code Private Sub Button1_Click(ByVal sender As System.Object, _                         ByVal e As System.EventArgs) _ Handles Button1.Click End Sub with: 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(&quot;Word.Application&quot;) 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(&quot;Author&quot;).Value MsgBox(&quot;The author of this document is &quot; & strValue)

'Set the value of the Subject property. oBuiltInProps.Item(&quot;Subject&quot;).Value = _ &quot;Knowledge Base article Q303294&quot;

'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(&quot;Knowledge Base article&quot;, False, _                 Office.MsoDocProperties.msoPropertyTypeString, &quot;Q303294&quot;)

'Display a message box to give the user a chance to verify the 'properties. MsgBox(&quot;Select Properties from the File menu &quot; _              & &quot;to view the changes.&quot; & Chr(10) _               & &quot;Select the Summary tab to view &quot; _               & &quot;the Subject and the Custom tab to view the Custom &quot; _               & &quot;properties.&quot;, MsgBoxStyle.Information, _               &quot;Check File Properties&quot;)

'Clean up. We'll leave Word running. oCustomProps = Nothing oBuiltInProps = Nothing oDoc = Nothing oWord = Nothing

End Sub </li>  Add the following code to the top of Form1.vb: Imports Office = Microsoft.Office.Core Imports Word = Microsoft.Office.Interop.Word </li> Press F5 to run the application.</li> Click Button1 to start Microsoft Word.</li></ol>

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 &quot;Knowledge Base article Q303294,&quot; and creates a new custom document property that is named &quot;Knowledge Base article.&quot; When you are prompted to view the changes, switch to Word, and click Properties on the File menu.

<div class="references_section">