The following code lists all variables in a word document:
It didn't work so I created a document on the fly with several docvariables to test and it worked great. Can anyone tell me why the code wouldn't work with one document but it would with another? The one it won't work with probably has about one hundred docvariables and it also has many bookmarks.
Code:
Dim curDoc As Document
Dim strDocVariable As String
Dim objDocVariable As Variable
Dim intcounter As Integer
strDocVariable = vbNullString
Set curDoc = ActiveDocument ' Application.ActiveDocument
MsgBox ActiveDocument.Variables(1).Name
With curDoc
For intcounter = 1 To .Variables.Count
strDocVariable = strDocVariable & CStr(intcounter) & ".) " & .Variables.Item(intcounter).Name
strDocVariable = strDocVariable & vbNewLine
Next 'intCounter
If .Variables.Count > 1 Then
Set newDoc = Application.Documents.Add
newDoc.Range().Text = strDocVariable
End If
End With
'use another way to get them
intcounter = 1
For Each objDocVariable In curDoc.Variables
Debug.Print curDoc.Variables.Item(intcounter).Name
intcounter = intcounter + 1
Next
Set curDoc = Nothing
It didn't work so I created a document on the fly with several docvariables to test and it worked great. Can anyone tell me why the code wouldn't work with one document but it would with another? The one it won't work with probably has about one hundred docvariables and it also has many bookmarks.