Word document properties give you an easy way to change variable data Ms Access Gurus

Help with costs for this site, thank you.

VBA to list Word Document Property names and values and Reference how to use Word Built-in and Custom Document Properties

Word Document Properties (Built-in and Custom) are a great way to substitute data in your document all over! ... in many places or just one.

This document shows a gray background for text that is coming from a field. In this document, all the embedded fields contain document properties, but there are many other fields you can use. As an aside, a field I use a lot is STYLEREF, particularly in headers and footers, to show dynamic content from Heading 1 and Heading 2 styles ... as well as Pages and NumPages.

When a value is changed in the document properties, the updated document text will show the new values anywhere a document property is referenced, after the fields are updated (future article on this).

Updated VBA June 2023: Choose to sort document properties in alphabetical order, or not. Important Built-in properties that can be displayed in columns of Windows File Explorer are bold. Specify to document just Built-in document properties, Custom document properties, or both (default).

insert document properties in Word

Quick Jump

Goto the Very Top  


Steps

To edit Document Properties, choose File, Info, and click Advanced Properties

File, Info, click Advanced Properties

There are 2 types of properties -- Built-in and Custom

Built-in Properties

Change Built-in property values one of 2 ways:

  1. instead of clicking to popup the dialog box for Advanced Properties, scroll to the bottom of the properties listed, and click Show All Properties (what you want to change might not displayed), and change values directly.

    File, Info, Properties, click Show All Properties at the bottom

    Click in the value that you want to change select Built-in Comments property to edit

    and type your new value type new value for Comments

  2. OR, popup dialog for Advanced Properties, and go to the Summary tab to make changes to basic built-in property values.

    edit Built-in properties for Word documents

Display Built-in Properties in File Explorer

Several of the built-in properties can be displayed in columns of File Explorer. In Details view, right-click a column heading and choose "More..." from bottom of the list.

File Explorer customize columns to display

Then check the details you want to see.

File Explorer columns to display

File Explorer columns can display Title and other document properties

Custom properties

From the Advanced Properties dialog box, go to the Custom tab to create and change custom property values. I generally preface custom properties with 'a' so they're listed first and it's easy to see that it's a custom property. Note that even though Date can also have a time, I didn't see a way to put that in using this interface.

edit Custom properties for Word documents

Because of the issue to update DATE and TIME in the user interface, I included a VBA procedure called SetCustomProperty_s4p. And also because of this, I decided to make separate properties with Time strings.

Goto Top  


HowTo

How to insert a field containing a document property value

From the ribbon, one way, is to choose Insert, Quick Parts, Field... Insert Field is under Quick Parts

I prefer to add Insert Field to my QAT (Quick Access Toolbar) since I use it so much. Insert Field can be added to QAT watch QAT video (12:52)

After you choose to insert a field, a dialog box pops up. On the left, choose DocProperty (type D on the keyboard to go closer quicker) and then click the field you want in the Property list. Insert DocProperty Field

It would be nice if this dialog box also showed current values, but it doesn't.

Field Codes

Format is to display information in a particular order and in a particular way. This is especially useful for dates. Format doesn't alter a value, it only changes how it's displayed.

If you're picking a field that you want to specify a format code for, after selecting, click Field Codes in lower left. Insert Date Due DocProperty Field, click Field codes to set format

after the field, type, for instance, \@ then space then format code in quotes, like: \@ "dddd, MMMM d, yyyy"

Capital M is for Month whereas lowercase m is for minute. This code shows the day name, comma, space, month name, space, day number, comma, space, and 4-digit year. Insert Date Due Field and specify format

Goto Top  

Update Property Values used in your document

After changing property values, select fields in the document (or press Ctrl-A to select entire document) and press F9 to update fields.

You can also right-click on a field or selection (such as the entire document) and choose Update Field. right-click and choose Update Field

Goto Top  

Toggle Field Codes

Note that you can also use the selection context menu to Toggle Field Codes (for the selection) instead of having to set an option backstage. Press Ctrl-A to select whole document first if you want all field codes to toggle between showing value or showing the field code. Sometimes though, you do just want to see one field code, or all the field codes in a paragraph.

Show Values text of fields

Show Field Codes field codes for fields

When you show field codes, pagination won't be right since they take more space to display. An advantage is that field codes can be easily edited when you can see them.

Goto Top  

Display Field Shading, Show Field Codes

To change how field shading is used on your document, go to Options, Advanced. In the Show document content section, set Field shading to Always, Never, or When selected.

use Options to set how fields appear in Word documents

In this section, you can also choose to show field codes instead of their values.

Get VBA to change how field shading is displayed in your document. Word Field Shading

Goto Top  


Download

Download demo Word Document with values for Built-in and Custom properties that are used in the document by inserting them as fields: Word_DocProperty_demo__DOCX.zip

There are places for fields with properties to go under Important Dates so you can get practice doing that yourself. How to insert fields with property values

The download document doesn't have any code -- just defined document properties, and fields in the document that reference them. I usually put code in the Normal.dotm template file so it's accessble from any document you open.

then, in either case, compile, (fix,) and save.

If you have trouble with a download, you probably need to UNBLOCK and remove Mark of the Web from each file before using or extracting. Here are steps to do that: https://msaccessgurus.com/MOTW_Unblock.htm

Goto Top  


Video

Watch on YouTube: Word Document Properties and Fields (14:05)

Goto Top  

VBA

This code lists built-in and custom properties with their values at the end of the active document. You can choose to see just Built-in, just Custom, or both.

I generally put code like this in Normal.dotm so it can run on any document.

'*************** Code Start *****************************************************
' module name: mod_List_Word_DocProperties_s4p
'-------------------------------------------------------------------------------
' Purpose  : VBA to list:
'              BuiltInDocumentProperties
'                 and/or
'              CustomDocumentProperties
'            at the end of the ActiveDocument.
'            Choose to sort alphabetically or not
' Author   : crystal (strive4peace)
' Code List: www.msaccessgurus.com/code.htm
' This code: https://msaccessgurus.com/VBA/Word_DocumentProperties.htm
' LICENSE  :
'   You may freely use and share this code, but not sell it.
'   Keep attribution. Use at your own risk.
'-------------------------------------------------------------------------------
' NOTE: if you put this code into Normal.dotm,
'       it will be available for any document
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'           List_WordDocumentProperties_s4p
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sub List_WordDocumentProperties_s4p() 
'220614 s4p, 220710 ... 220831, 230612, 17
   ' CALLs
   '  DoTableBorders_s4p
   
   '-------------------------------------- CUSTOMIZE!
   Const bSORT As Boolean = True  'sort properties alphabetically
   Const sOPTIONS As String =  "BC" 'B=Built-in, C=Custom
'   Const sOPTIONS As String = "C" 'C=Custom
   '--------------------------------------
   
   '----- dimension object variables
   Dim oDoc As Word.Document 
   Dim oRange As Range 
   Dim oTable As Word.Table 
   
   Dim oDocProp As DocumentProperty _ 
      ,oContainer As DocumentProperties 
   
   '----- dimension scalar variables
   Dim iBC As Integer 
   
   Dim sContainerName As String _ 
      ,sTypeContainer As String _ 
      ,sBuiltinCustom As String _ 
      ,sProperty As String 
      
   Dim nRows As Long _ 
      ,nCols As Long _ 
      ,nRow As Long 
      
   Dim vMessage As Variant  '230612
   
   'important Built-in properties that can be displayed
   'in columns of Windows File Explorer will be bold
   Dim asBold(1 To 2) As String  '230612
   asBold(1) =  "~Title~Author~Company~Comments~Subject~"
   asBold(2) =  ""
         
   '----- set oDoc to ActiveDocument
   Set oDoc = Word.ActiveDocument 

   '----- write information
   'insert new line at end of current document
   'write Document Properties as Heading 2
   With oDoc.Content 
      'collapse to end of document
      .Collapse Direction:=0  'wdCollapseEnd

      'blank line
      .InsertParagraphAfter 
      
      .InsertAfter  "Document Properties " _ 
         & IIf(bSORT, "(sorted)", "") &  ": " _ 
         & Format(Now(), "yymmdd hh:nn ") _ 
         & oDoc.Name 
      'style as Heading 2
      oDoc.Paragraphs(oDoc.Paragraphs.Count).Style _ 
            = oDoc.Styles( "Heading 2") 
      .InsertParagraphAfter 
      
      .InsertAfter  "file: " & oDoc.FullName 

      .InsertParagraphAfter 
            
   End With  'oDoc.Content
   
   'skip errors in case a property doesn't have a value
   On Error Resume Next 
   
   'iterate DocumentProperties
   ' Built-in (1) and Custom (2)
   For iBC = 1 To 2 
      With oDoc 

         If iBC = 1 Then 
            If Not InStr(sOPTIONS, "B") > 0 Then 
               GoTo Next_Option 
            End If 
            sTypeContainer =  "B" 'built-in
            sBuiltinCustom =  "Built-in"
            Set oContainer = .BuiltInDocumentProperties 
            sContainerName =  "BuiltInDocumentProperties"
         Else  '2
            If Not InStr(sOPTIONS, "C") > 0 Then 
               Exit For 
            End If 
            sTypeContainer =  "C" 'custom
            sBuiltinCustom =  "Custom"
            Set oContainer = .CustomDocumentProperties 
            sContainerName =  "CustomDocumentProperties"
         End If 
      
         'count properties
         nRows = oContainer.Count 
         
         With .Content 
            'collapse to end of document
            .Collapse Direction:=0  'wdCollapseEnd
            
            'blank line
            .InsertParagraphAfter 
            
            'Specify Built-in or Custom
            .InsertAfter sContainerName 
            If bSORT Then 
               .InsertAfter  "      (sorted alphabetically)"
            End If 
            'style as Heading 3
            oDoc.Paragraphs(oDoc.Paragraphs.Count).Style _ 
                  = oDoc.Styles( "Heading 3") 
            .InsertParagraphAfter 
            
         End With  'oDoc.Content
                 
         'range for table, put at end
         Set oRange = .Content 
         oRange.Collapse Direction:=0  'wdCollapseEnd
         
         'insert table
         nCols = 3  'number of columns
         'NumRows: number of properties + 1 for header row
         Set oTable = .Tables.Add( _ 
            Range:=oRange _ 
            ,NumRows:=nRows + 1 _ 
            ,NumColumns:=nCols _ 
            ) 
      End With  'oDoc
  
      'customize and write table
      With oTable 
      
         'dont allow rows to break
         .Rows.AllowBreakAcrossPages = False 
         
         'Vertical Alignment for each cell is Top
         ' 0=wdCellAlignVerticalTop
         .Range.Cells.VerticalAlignment = 0 
         
         'heading row
         .Rows(1).HeadingFormat = True 
         .Cell(1,1).Range.Text = sBuiltinCustom &  " Document Property Name"
         .Cell(1,2).Range.Text =  "DataType"
         .Cell(1,3).Range.Text =  "Value"
         
         nRow = 1  'header row just written
         
         'loop through properties and write values
         For Each oDocProp In oContainer 
            sProperty = oDocProp.Name 
            nRow = nRow + 1  'go to next row
            .Cell(nRow,1).Range.Text = sProperty 
            
            '230612
            If InStr(asBold(iBC), "~" & sProperty &  "~") Then 
               'bold the text in the cell
               .Cell(nRow,1).Range.Font.Bold = True 
            End If 
            
            .Cell(nRow,2).Range.Text = TypeName(oDocProp.Value) 
            .Cell(nRow,3).Range.Text = oDocProp.Value 
         Next oDocProp 

         'best-fit columns
         .Columns.AutoFit 
         
         'format
         With .Range.ParagraphFormat 
            .SpaceAfter = 0 
            .SpaceBefore = 0 
            .LineSpacing = 0  ' wdLineSpaceSingle
         End With  'ParagraphFormat
   
         If bSORT Then 
            'sort table by property name for Custom
            ' leave Built-in properties sorted logically
'            If iBC = 2 Then
               .Sort ExcludeHeader:=True _ 
                  ,FieldNumber:=1 
'            End If
         End If 
         
      End With  'oTable
         
      'add table borders
      Call DoTableBorders_s4p(oTable) 
               
      With oDoc.Content 
         'go to end of document
         .MoveEnd unit:=wdStory 
      
'         'add blank line before count
'         .InsertParagraphAfter
         
         'write how many properties were found
         .InsertAfter  "** " _ 
            & Format(nRows, "0;;\n\o\n\e") _ 
               &  " " & sBuiltinCustom _ 
               &  " Document Properties listed"
            
      End With  'oDoc.Content
Next_Option: 
      
   Next iBC  'next Built-in or Custom list
   
   vMessage = Null 
   If InStr(sOPTIONS, "B") > 0 Then 
      vMessage =  "Built-in "
   End If 
   If InStr(sOPTIONS, "C") > 0 Then 
      vMessage = (vMessage +  "and ") &  "Custom "
   End If 
   vMessage =  "Done enumerating " & vMessage _ 
      &  "Document Properties"
   
   MsgBox vMessage,, "done"
   
Proc_exit: 
   'release object variables
   Set oDocProp = Nothing 
   Set oContainer = Nothing 
   Set oTable = Nothing 
   Set oRange = Nothing 
   Set oDoc = Nothing 
   
End Sub 
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'           DoTableBorders_s4p_s4p
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Public Sub DoTableBorders_s4p(oTable As Object)   'Word.Table
's4p 170811
   Dim i As Integer 
   With oTable 
      For i = 1 To 6 
         'wdBorderTop =-1
         'wdBorderLeft = -2
         'wdBorderBottom =-3
         'wdBorderRight= -4
         'wdBorderHorizontal = -5
         'wdBorderVertical = -6
         With .Borders(-i) 
            .LineStyle = 1   'wdLineStyleSingle=1
            .LineWidth = 8  'wdLineWidth100pt=8. wdLineWidth150pt=12
            .Color = RGB(200,200,200)  'medium-light gray
         End With 
      Next i 
   End With 
   'change borders to black for first row
   With oTable.Rows(1) 
      For i = 1 To 4 
         With .Borders(-i) 
            .Color = 0       'wdColorBlack = 0
         End With 
      Next i 
      'Shading for header row
      .Shading.BackgroundPatternColor = RGB(232,232,232) 
   End With  'first row
   'Not used:
'      'wdLineStyleNone = 0
'      .Borders(-7).LineStyle = 0 'wdBorderDiagonalDown =-7
'      .Borders(-8).LineStyle = 0 'wdBorderDiagonalUp =-8
End Sub 
'*************** Code End *******************************************************
' Code was generated with colors using the free Color Code add-in for Access.

Goto Top  

VBA Results

When you run the VBA code on your document, you will get tables at the end of the document showing names and values of properties.

Properties can be sorted in alphabetical order, or not. Modify the new CUSTOMIZE! section. In addition to sorting, you can choose to show Built-in and/or Custom properties.

Built-in document properties

Important Built-in properties that can be displayed in columns of Windows File Explorer, and can be changed, are bold.

Built-in properties list

Custom document properties

Peoperty names can be sorted alphabetically

Custom properties list

Goto Top  

Reference

related articles on MsAccessGurus

WordAutomate: Word Automation VBA Code to download a database with VBA and examples creating Word documents from Access.

Get VBA to change how field shading is displayed in your document. Word Field Shading

Microsoft Help

Help: Built-inDocumentProperties

Help: CustomDocumentProperties

Help: DocumentProperties object

Help: DocumentProperty object

Help: Tables collection

Help: Table object

Help: Range object

Help: Collapse method

Videos

Word Document Properties and Fields (14:05)

Customize Quick Access Toolbar (QAT), and import/export QAT definitions (12:52)

Merge to Word using Access as a data source into a Letter or whatever else (17:10)

Goto Top  

Backstory

Word document properties give you a powerful way to substitute data, but the user interface to manage them is terrible! I'm creating an application in Access to set and see document properties in Word. Document properties are wonderful to update variable information and it doesn't matter how many places it is used! You don't have to search and replace. Simply change the property value and then Update Fields in your document. So easy!

  1. create a new module in your Normal.dotm template, copy and paste the VBA code, name the module mod_List_Word_DocProperties_s4p then compile, and save Normal.
  2. Download zip file with sample Word document
  3. Unblock the zip file
  4. extract Word document from the zip file
  5. Open Word document
  6. Run the code List_WordDocumentProperties_s4p and look at the results!
  7. There is an empty table in the demo Word document for important dates so you can fill them out yourself by inserting DocProperty fields. Experiment with format codes (smile). Maybe you also want to edit property values (File, Info) and then update the document (select all, F9)? Can this technique be useful for you?

I still also use other methods like bookmarks to mark positions for inserting external images, creating multiple records, and other special cases.

Here's a good reference for Word Automation VBA

If you like this page, please let me know. Donations are much appreciated, thank you

Share with others

here's the link for this page in case you want to copy it:
https://msaccessgurus.com/VBA/Word_DocumentProperties.htm

Get Help with Access and Word

Let's connect and team-develop your application together. I teach you how to do it yourself. My goal is to empower you.

While we build something great together, I'll pull in code and features from my vast libraries as needed, cutting out lots of development time. I share links with you for valuable resources.

Do you have information in a database that you want to insert into Word documents? Or do you have a bunch of documents that you're constantly manually changing? Do you want to streamline your process? Email me at training@msAccessGurus.com

~ crystal

the simplest way is best, but usually the hardest to see

Goto Top