How to Read Word Document Properties Programmatically via VBA Macro
🔍 WiseChecker

How to Read Word Document Properties Programmatically via VBA Macro

Document properties in Word store metadata such as author name, title, subject, keywords, and last-saved date. You may need to extract this information automatically when processing multiple files or generating reports. The built-in document properties panel shows this data, but reading it with a VBA macro lets you collect it in bulk or use it in other applications. This article explains how to write a VBA macro that reads both built-in and custom document properties from a Word document.

Key Takeaways: Reading Word Document Properties With VBA

  • ActiveDocument.BuiltInDocumentProperties: Accesses standard metadata like author, title, and creation date.
  • ActiveDocument.CustomDocumentProperties: Reads user-defined properties that you or others added to the document.
  • For Each loop with property.Value: Iterates through all properties and retrieves their current values without error.

ADVERTISEMENT

Understanding Word Document Properties in VBA

Word stores two types of document properties: built-in and custom. Built-in properties are predefined by Microsoft and include fields like Author, Title, Subject, Keywords, Comments, Last Save Time, Creation Date, and Total Editing Time. Custom properties are created by users or other applications and can hold text, numbers, dates, or Yes/No values. Both types are accessible through the Document object in the VBA object model.

To read these properties, you use the BuiltInDocumentProperties and CustomDocumentProperties collections. Each property in these collections has a Name, Value, and Type property. The Value property may raise an error if the property is empty or contains a data type that VBA cannot interpret directly. You must handle this with error trapping or type checking.

Before running any macro that reads properties, ensure the document is saved at least once. Properties such as Last Save Time and Total Editing Time are only populated after a save operation. Also, the macro must be run from the document itself or from a template that is attached to the document. The ActiveDocument object refers to the currently open document in the Word application window.

Writing the VBA Macro to Read Document Properties

Method 1: Reading a Specific Built-In Property

  1. Open the VBA Editor
    Press Alt+F11 in Word to open the Visual Basic for Applications editor. If the editor opens but the Project Explorer is hidden, press Ctrl+R to show it.
  2. Insert a new module
    In the Project Explorer, find your document name (for example, Normal or Document1). Right-click it and choose Insert > Module. A blank code window appears.
  3. Create the macro with a Sub procedure
    Type the following code into the module window. This macro reads the Author property and displays it in a message box.
    Sub ReadAuthorProperty()
        Dim authorValue As String
        On Error Resume Next
        authorValue = ActiveDocument.BuiltInDocumentProperties("Author").Value
        If Err.Number <> 0 Then
            authorValue = "Not available"
            Err.Clear
        End If
        MsgBox "Author: " & authorValue
    End Sub
  4. Run the macro
    Press F5 while the cursor is inside the ReadAuthorProperty procedure. A message box displays the author name. If the property is empty, the output shows “Not available”.

Method 2: Looping Through All Built-In Properties

  1. Add a new macro to the same module
    Below the previous macro, type the following code. This macro iterates through every built-in property and prints its name and value to the Immediate Window.
    Sub ListAllBuiltInProperties()
        Dim prop As DocumentProperty
        Dim propValue As Variant
        
        For Each prop In ActiveDocument.BuiltInDocumentProperties
            On Error Resume Next
            propValue = prop.Value
            If Err.Number <> 0 Then
                propValue = "Empty or unsupported type"
                Err.Clear
            End If
            Debug.Print prop.Name & ": " & propValue
        Next prop
        
        MsgBox "Properties listed in Immediate Window. Press Ctrl+G to view."
    End Sub
  2. Open the Immediate Window
    Press Ctrl+G in the VBA editor to open the Immediate Window. Run the ListAllBuiltInProperties macro by placing the cursor inside it and pressing F5.
  3. Review the output
    The Immediate Window shows each property name followed by its value. Properties without a value display “Empty or unsupported type”.

Method 3: Reading Custom Document Properties

  1. Add a macro for custom properties
    Type the following code in the same module. It reads all custom properties and displays them in a message box.
    Sub ListCustomProperties()
        Dim prop As DocumentProperty
        Dim output As String
        Dim propValue As Variant
        
        output = "Custom Properties:" & vbCrLf
        
        For Each prop In ActiveDocument.CustomDocumentProperties
            On Error Resume Next
            propValue = prop.Value
            If Err.Number <> 0 Then
                propValue = "Empty or unsupported type"
                Err.Clear
            End If
            output = output & prop.Name & ": " & propValue & vbCrLf
        Next prop
        
        MsgBox output
    End Sub
  2. Run the macro
    Place the cursor inside ListCustomProperties and press F5. A message box lists all custom properties and their values.

Method 4: Exporting Properties to a Text File

  1. Create a macro that writes to a file
    This macro saves both built-in and custom properties to a text file on your desktop. Add the following code to the module.
    Sub ExportPropertiesToFile()
        Dim prop As DocumentProperty
        Dim filePath As String
        Dim fileNum As Integer
        Dim propValue As Variant
        
        filePath = Environ("USERPROFILE") & "\Desktop\DocumentProperties.txt"
        fileNum = FreeFile
        
        Open filePath For Output As #fileNum
        
        Print #fileNum, "Built-In Properties:"
        For Each prop In ActiveDocument.BuiltInDocumentProperties
            On Error Resume Next
            propValue = prop.Value
            If Err.Number <> 0 Then
                propValue = "Empty"
                Err.Clear
            End If
            Print #fileNum, prop.Name & " = " & propValue
        Next prop
        
        Print #fileNum, ""
        Print #fileNum, "Custom Properties:"
        For Each prop In ActiveDocument.CustomDocumentProperties
            On Error Resume Next
            propValue = prop.Value
            If Err.Number <> 0 Then
                propValue = "Empty"
                Err.Clear
            End If
            Print #fileNum, prop.Name & " = " & propValue
        Next prop
        
        Close #fileNum
        MsgBox "Properties exported to " & filePath
    End Sub
  2. Run the export macro
    Press F5 with the cursor inside ExportPropertiesToFile. A text file named DocumentProperties.txt appears on your desktop. Open it with Notepad to see the full property listing.

ADVERTISEMENT

Common Issues When Reading Document Properties via VBA

Property Value Returns an Error or Empty String

Some built-in properties are not populated until the document is saved. For example, Last Save Time shows an error if the document has never been saved. Use On Error Resume Next before accessing the Value property and check Err.Number afterward. If the property is empty, assign a default string such as “Not available”.

The Macro Cannot Find a Specific Property by Name

Property names are case-insensitive but must match exactly in spelling. For built-in properties, use the English name as it appears in the object model: Author, Title, Subject, Keywords, Comments, Last Save Time, Creation Date, Last Printed, Revision Number, Total Editing Time, Number of Pages, Number of Words, Number of Characters, Security, Category, Manager, Company, and Hyperlink Base. For custom properties, the name must match the exact string used when the property was created.

Custom Properties Are Not Visible in the Document Panel

Custom properties added by other applications may use a different data type that VBA cannot read directly. The Value property returns a Variant, but if the underlying data is a date or a Boolean, the conversion may fail. Use the Type property of the DocumentProperty object to determine the data type before reading the value. The type constants are msoPropertyTypeNumber, msoPropertyTypeBoolean, msoPropertyTypeDate, msoPropertyTypeString, and msoPropertyTypeFloat.

Built-In Properties vs Custom Properties: Access and Behavior

Item Built-In Properties Custom Properties
Predefined set Yes, fixed by Microsoft No, user-defined
Access method ActiveDocument.BuiltInDocumentProperties ActiveDocument.CustomDocumentProperties
Name uniqueness Names are fixed and cannot be duplicated Names must be unique per document
Data types supported String, Date, Number (varies by property) String, Number, Date, Boolean, Float
Error on empty value Yes, requires On Error Resume Next Yes, requires On Error Resume Next
Can be modified via VBA Yes, some are read-only Yes, all are writable
Persist after save Yes Yes

Now you can write a VBA macro to read document properties from any Word file. Start with the code examples above and adjust them for your specific property names. For advanced use, consider looping through all open documents with Documents collection or reading properties from closed files using the GetObject method. One concrete tip: always use On Error Resume Next and check Err.Number for each property access to avoid runtime crashes when properties are missing.

ADVERTISEMENT