Listing Field Values used in a document

  • Thread starter Thread starter Lincoln
  • Start date Start date
L

Lincoln

Hello,

I have received a document with a ton of fields. I would
like to find a way to extract the field names and
properties to a separate list for review. Can this be
done? I'm using Word 2002.

Thanks in advance for any guidance.
 
You could use this macro, which lists all fields in the document in the
immediate window. You can cut and paste from there.

Sub ListFields()

Dim pfield As Word.Field
Dim pRange As Word.Range

For Each pRange In ActiveDocument.StoryRanges
Do
For Each pfield In pRange.Fields
Debug.Print pfield.Code, pfield.Type, pfield.Kind
Next
Set pRange = pRange.NextStoryRange
Loop Until pRange Is Nothing
Next

End Sub
 
Hi Lincoln,

The following code generates a list of all bookmarks at the end of the
active document, and displays their contents. If you need to do more, you
could incorporate other fields by changing ActiveDocument.Bookmarks to
ActiveDocument.Fields, though some (eg Date) won't have names.



Cheers



Sub GetBkMrks()
If ActiveDocument.Bookmarks.Count > 0 Then
For Each oBmk In ActiveDocument.Bookmarks
With Selection
.EndKey Unit:=wdStory
.InsertAfter oBmk.Name & " "
.EndKey Unit:=wdStory
oBkMrk =
ActiveDocument.Fields.Add(Range:=Selection.Range, Text:=oBmk.Name,
PreserveFormatting:=False)
.InsertAfter vbCrLf
End With
Next oBmk
End If
End Sub
 
Substituting 'Fields' for 'Bookmarks' won't work with this code. First,
fields don't have a Name property, so you won't get any output, just errors.
Second, it would miss all the fields not in the body of the document (eg
headers, footers, footnotes, textboxes, etc).
 
Hi Jezebel,
Yes, you're right about both issues. To address the first issue, the
following code would work:

Sub ListFields()
Dim oFld As Field
If ActiveDocument.Fields.Count > 0 Then
For Each oFld In ActiveDocument.Fields
With Selection
.EndKey Unit:=wdStory
.InsertAfter vbCrLf
.EndKey Unit:=wdStory
.TypeText Text:="{" & oFld.Code & "}, " & oFld.Result
End With
Next oFld
End If
End Sub

To address the second issue, additional code could be used to loop through
headers, footers & shapes as with the following field updating macro (which
might be useful to run before running ListFields):

Sub RefreshFields()
Dim oSection As Section, shp as Shape, oHeadFoot As HeaderFooter
ActiveDocument.Fields.Update
For Each oSection In ActiveDocument.Sections
For Each oHeadFoot In oSection.Footers
If Not oHeadFoot.LinkToPrevious Then oHeadFoot.Range.Fields.Update
Next
For Each oHeadFoot In oSection.Headers
If Not oHeadFoot.LinkToPrevious Then oHeadFoot.Range.Fields.Update
Next
Next
If ActiveDocument.Shapes.Count > 0 Then
For Each shp In doc.Shapes
With shp.TextFrame
If .HasText Then .TextRange.Fields.Update
End With
Next
End If
End Sub

Cheers
 
Seems to me the code I posted in my original response is a simpler solution
still, that solves both problems. Replace the debug.print with Insert
statements if you want the output into a document --

Dim pfield As Word.Field
Dim pRange As Word.Range

For Each pRange In ActiveDocument.StoryRanges
Do
For Each pfield In pRange.Fields
Debug.Print pfield.Code, pfield.Type, pfield.Kind
Next
Set pRange = pRange.NextStoryRange
Loop Until pRange Is Nothing
Next
 
Back
Top