Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

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.

↑ Back to the top


More information

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.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("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.Core
    Imports 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.

↑ Back to the top


References

For more information, visit the following Microsoft Developer Network Web site:
Microsoft Office Development with Visual Studio
http://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx
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

↑ Back to the top


Keywords: KB303294, kbhowto, kbautomation, kbpia

↑ Back to the top

Article Info
Article ID : 303294
Revision : 11
Created on : 6/29/2007
Published on : 6/29/2007
Exists online : False
Views : 585