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

Support for Office 2003 has ended

Microsoft ended support for Office 2003 on April 8, 2014. This change has affected your software updates and security options. Learn what this means for you and how to stay protected.

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

  1. Start Visual Studio .NET.
  2. On the File menu, click New, and then click Project. Select Windows Application from the Visual Basic Project types. Form1 is created by default.
  3. Add a reference to Microsoft Word Object Library. To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. 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
    3. 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.
  4. On the View menu, select Toolbox to display the toolbox, and add a button to Form1.
  5. Double-click Button1. The code window for the form appears.
  6. 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 					
  7. Add the following code to the top of Form1.vb:
    Imports Office = Microsoft.Office.CoreImports Word = Microsoft.Office.Interop.Word
  8. Press F5 to run the application.
  9. 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 Office Development with Visual Studio
For more information, click the following article number to view the article in the Microsoft Knowledge Base:
303296 How to use Automation to get and set Office document properties with Visual C# .NET

Article ID: 303294 - Last Review: 12/06/2015 03:53:20 - Revision: 7.4

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

  • kbnosurvey kbarchive kbpia kbautomation kbhowto KB303294