10

I have document with some document variables in and I'm trying to use those variables in the document. But since the variables are set by an external system (which is not well documented) I'm playing a guess-game. I assume a variable with "last revision date" exists and then start guessing what that variable might have been called.

Is there a way to list all the document variables in a MS Word document and their values so that one can look them up when using them in the document? (I'm working on MS Office 2010)

3 Answers 3

13

Word has a number of different types of "variable", There are things known as "Document Variables", but there are other things that I will term "properties" - I'll try to list the main types below.

There are a few places in Word you can look to find some types of properties, but for true "variables" and other types of properties, if you can't use macros or other code, you would need to unzip the .docx file and look at the various files in there.

Places you can look within Word 2010 (no Document Variables are displayed here)...

a. In "Backstage" - look at the File tab, Info option, and you should see some properties listed on the right. Click "Show All Properties" at the bottom of the list and you may see more.

b. In the Insert tab, Text group, Quick Parts->Properties, you should see a list of "Built in properties + some or all properties inserted by Sharepoint if that is what you are using.

c. In the "Document Information Panel" (DIP). If you do not see that, go into the File menu then Options, then (say) Quick Access Toolbar. Select "Choose Commands" under "All Commands", then look for "Properties", select it, and click the Add button. You should then see an icon in the Quick Access toolbar that has a small white "i" against a circular blue background. Click that, and you should see either some builtin properties (Author, Title etc.) or some Sharepoint "Content type" properties, if you are using SharePoint. You may be able to flip between one of several views in there. If you then click the dropdown at the top left of the DIP and choose "Advanced Properties", you will see a multi-tab dialog box which shows

  • some "builtin" properties

  • old-style "user-defined" Custom Document Properties.

For everything else, you'll probably have to look in the XML in the .docx. To do that, make a copy of your document. Let's say the copy is called mydoc.docx. Rename that to mydoc.docx.zip, open the zip, then look for a number of things, as suggested below...

a. true Document Variables are stored in the word folder, in settings.xml, in an element called w:docVars. You should see a name and a value for each variable. You can insert the values of Document Variables in your document using a { DOCVARIABLE variablename } field.

b. Builtin document properties, including such things as author, title etc. In most cases, the values can be inserted in the document using spcialised fields such as { AUTHOR }, or in some cases { DOCPROPERTY } fields. Although it is a bit messy, there is a standard list of names. You can find most of the values in the docProps folder in the core.xml and app.xml files. However, some (the "Cover Page Properties") are stored, if used, in one of the "item.xml" files (e.g. "item1.xml" in the customXml folder.

c. old-style "user-defined" custom document properties which consist mainly of name/value pairs whose values can be inserted in the document using { DOCPROPERTY } fields, e.g. { DOCPROPERTY mycustomproperty }. These are displayed in the Advanced Properties.... dialog, but in the .docx they are in the custom.xml file in the docProps folder.

d. new-style custom properties, also known as "Sharepoint properties" or "content type properties," because they are often created and populated from columns in a Sharepoint list. The values are stored in "Custom XML Parts" and can be inserted using Content Controls which have been set up to point to the relevant bit of XML. (Word 2007 and later only). In the .docx, you will find a number of "schemas" for these variables, and the values of the variables in the customXML folder, in the files named "item1.xml", "item2.xml" etc.

e. finally, any developer can put their own custom XML parts into a Word document, and connect up Content Controls to elements and attributes in those custom XML parts. I won't attempt to go into the details of how that is done, but you will find the data in the item1.xml, item2.xml etc. files under customXML. However, to find the content controls themselves, their names and what they are connected too, you would typically need to look in the word folder at, e.g. document.xml, and perhaps one or more of the "header" and "footer" .xml files.

3
  • Great answer. Thanks! One note: "b. In the Insert tab, Text group, Quick Parts->Properties, you should see a list of "Built in properties + some or all properties inserted by Sharepoint if that is what you are using." In my setup Quick Parts doesn't seem to have "Properties"
    – some user
    May 16, 2013 at 7:54
  • My error - that should be Quick Parts->Document Property
    – user181946
    May 19, 2013 at 19:16
  • Properties and DocVariables seem pretty similar in how you can use them in word fields and VBA. Are there any tradeoffs?
    – BenPen
    Nov 4, 2016 at 20:02
5

I was having a similar problem. Some third party toolbox was crashing and I needed to set a variable value.

It's a bit crude but here is some VBA code I knocked up that will list all the variables in the ActiveDocument.Variables collection;

Public Sub ListAllVariables()
    Dim V As Variable, S As String
    For Each V In ActiveDocument.Variables
        S = S & V.Name & vbTab & V.Value & vbNewLine
    Next V
    MsgBox S
End Sub

You can create a new macro and dump it in there, which should give you the information. When you have found the name of the variable you are interested in, you can set it with

ActiveDocument.Variables("MyVariable") = "Value"
1
  • 3
    This is pretty cool, thanks. It's too bad that Microsoft didn't provide a pre-packaged sort of picker window that you give it a collection and you get a two column pop up window that lets you select one or more items from the collection (or add a new value to the actual collection) and then allows your VBA code to access the selected items in the calling sub/function.
    – BenPen
    Nov 4, 2016 at 20:01
1

Picking up on the line of Steztric: If you have more than 30+ docVariables you can display them in chuncks like so:

Public Sub ListAllVariables()
    Dim V As Variable, S As String, Counter As Integer
    Counter = 0
    For Each V In ActiveDocument.Variables
    If Counter < 20 Then
        S = S & V.Name & vbTab & V.Value & vbNewLine
        Counter = Counter + 1
    Else: MsgBox S
    Counter = 0
    S = " "
    End If
    Next V
  MsgBox S
End Sub

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .