Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listing Docvariables in a WORD document 2

Status
Not open for further replies.

pmcdaniel

Programmer
Feb 9, 2007
127
0
0
US
The following code lists all variables in a word document:
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.
 
Maybe, if you explain what "wouldn't work" means.

"Can anyone tell me why the code wouldn't work with one document but it would with another? "

You have the code:
Code:
       Set newDoc = Application.Documents.Add
but I don't see newDoc ever being declared.



faq219-2884

Gerry
My paintings and sculpture
 
I accidently took out the declaration.
Here's the code again:
Code:
Sub ListDocVariables()
  Dim curDoc As Document
  Dim newDoc As Document
  Dim strDocVariable As String
  Dim objDocVariable As Variable
  Dim intcounter As Integer
  
  strDocVariable = vbNullString
  Set curDoc = ActiveDocument ' Application.ActiveDocument

  With curDoc
    For intcounter = 1 To .Variables.Count
      strDocVariable = strDocVariable & CStr(intcounter) & ".) " & .Variables.Item(intcounter).Name
      strDocVariable = strDocVariable & vbNewLine
      'strDocVariable = strDocVariable & curDoc.Bookmarks(intCounter).Range.Text
    Next 'intCounter
    
    If .Variables.Count > 1 Then
        'Debug.Print strDocVariable
        Set newDoc = Application.Documents.Add
        newDoc.Range().Text = strDocVariable
    End If
  End With
  intcounter = 1
  For Each objDocVariable In curDoc.Variables
    Debug.Print curDoc.Variables.Item(intcounter).Name
    intcounter = intcounter + 1
  Next
  
  Set curDoc = Nothing
  Set newDoc = Nothing
    
End Sub

What I mean by it not working is that it would list all the docvariables from the test document I created but it didn't find any from the document with a lot of docvariables(.Variables.Count was equal to 0).
 
How do you know that the document has a lot of variables ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I have to ask that myself, especially if you say .Variables.Count = 0. VBA does not making many counting mistakes. If the .Count = 0, then it probably = 0.

faq219-2884

Gerry
My paintings and sculpture
 
Is this what you mean: CTRL+F9 inserts docvariables and this is what they look like.....
Code:
{docvariable varName1}
{docvariable varName2}
{docvariable varName3}
{docvariable varName4}
and these can be seen when the Field Codes check box is checked. There's probably about one hundred or so.

 
fumei; that's the way I figured it but I inserted a docvariable on my own the same way I did it on my test doc and it didn't get counted either.
 
Fields and Variables are not the same !
You may want to play with the Fields or FormFields collections.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
CTRL+F9 inserts docvariables and this is what they look like.....

Uh, Ctrl-F9 inserts fields!!! Those are NOT Variables!

I have a document with 60 fields made with Ctrl-F9, and the .Variables.Count = 0, because it IS zero.

faq219-2884

Gerry
My paintings and sculpture
 
Thanks but since I used CTRL+F9 for my test doc how come they were counted? Also, I had previously tried the Fields.count and it too was zero.

In any case I'll try other things. By the way how ARE docvariables inserted and how do they look on the document since what I thought were docvariables are actually fields?

In our VB app we're using AppWd.ActiveDocument.Variables.Item("varValue").Value to fill this document.

I appreciate you guys helping me with this.
 
I wonder if you used the DOCVARIABLE field type ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
The application I'm getting zeros on was created by a combination of other people and I was told they are docvariables. It was also explained to me that CTRL+F9 inserts docvariables. So I guess this just adds to the confusion.
 
Well I was doing something wrong because I repeated everything and the Fields.count is coming back with the values I was looking for!!!!

Thanks for your help.
 
There are DOCVARIABLE fields. They are members of the Fields collection.

There are document Variables. These are NOT fields. They are members of the Variables collection.

DOCVARIABLE fields can be seen in the document. Alt-F9 toggles the view. You see something like:

{DOCVARIABLE Yadda \* MERGEFORMAT }

Document variables (the things that .Variables.Count....counts) are NOT seen in the document.
Thanks but since I used CTRL+F9 for my test doc how come they were counted? Also, I had previously tried the Fields.count and it too was zero.
Sorry, but I simply do not believe this.


If you used Ctrl-F9, you made fields. Period. And I do not believe you got a Field.Count = 0, if you indeed used Ctrl-F9 - and did not delete them.

If you used Ctrl-F9 (and put in fields) and used code for Variables (and did not have any), then I simply do not believe the fields were counted.

They are different.

AppWd.ActiveDocument.Variables.Item("varValue").Value

is indeed a document variable, and would return the value of varValue. However, BY ITSELF, that line does not do much. Yes, it returns the value...but it does not put that value anywhere.

There must be some code somewhere that sets (Adds) the variable.
Code:
ActiveDocument.Variables.Add "Gerry", 12
MsgBox ActiveDocument.Variables.Item("Gerry").Value
displays a messagebox with "12".


By itself, even with the variable "Gerry" actually existing, and having a value, the code
Code:
ActiveDocument.Variables.Item("Gerry").Value
returns an error - "Invalid use of property".

Note that setting a document variable value replaces the existing value.

Bottom line? It looks like indeed you do have document variables. Try getting a count first. What do you get?

However, to reiterate - Ctrl-F9 are fields, not document variables....even if you use a DOCVARIABLE field. The DOCVARIABLE field is a method of displaying in the document the document variables...which by themselves do NOT show.

1. ActiveDocument.Variables.Add "Bob", "yadda yadda"
This adds a Variable "Bob". Its value is "yadda yadda"

2. IN the document, insert a DOCVARIABLE field, with the
name "Bob".

3. Alt-F9 to toggle the field codes. You see:
{DOCVARIABLE Bob \* MERGEFORMAT }

4. Toggle again (Alt-F9), and you see "yadda yadda"

Now it is peculiar, but if you toggle so you can see:

{DOCVARIABLE Bob \* MERGEFORMAT }

and go in and remove Bob, so it looks like:

{DOCVARIABLE \* MERGEFORMAT }, it will STILL toggle to "yadda yadda".

Further, if you change it - by typing in the field - to, say:

{DOCVARIABLE Harry Bellefonte \* MERGEFORMAT }

it will STILL toggle to "yadda yadda". Once the DOCVARIABLE field is set, it persists until updated. If you updated the field after the above changes, you will get an error. The blank, and the Harry Bellefonte variables do not exist.

As for your code:
Code:
  For Each objDocVariable In curDoc.Variables
    Debug.Print curDoc.Variables.Item(intcounter).Name
    intcounter = intcounter + 1
  Next
You do not need Item(intcounter).Name. objDocVariable.Name will work fine. It is looping through each. You do not need a counter at all.

I made a bunch of document variables, made DOCVARIABLE fields, and got correct values. Deleted ALL the fields, and the .Variables.Count remained correct, as there is no reverse connection.

So again, I would check your .Variables.Count first.

Also, finally, unless you have a more advanced evrsion, using Ctrl-F9 and entering a document variable name will NOT work. Even if you type in DOCVARIABLE.

{DOCVARIABLE Bob \* MERGEFORMAT } - with Insert > Field "Bob", yields "yadda yadda"

{DOCVARIABLE Bob \* MERGEFORMAT } - with Ctrl-F9, and entering the same text, yields "", ie. nothing.

So I am not sure what the heck you are doing with the Ctrl-F9. They will NOT (AFAIK) get the values of the document variables, even if named correctly.

faq219-2884

Gerry
My paintings and sculpture
 
Thanks much. This clears up some confusion on my part. I got confused with the terms DOCVARIABLE and document variables. The Variables collection are use in VBA(Visual Basic)?

It appears Ctrl+F9 inserts docvariable fields!? I just assumed they were simply variables in the document, thus DOCVARIABLES....shows how assuming can kill.

So DOCVARIABLES are FIELDS, correct?

Code:
ActiveDocument.Variables.Item("Gerry").Value = "Hello"
is the VB code used to fill the "Field" and thus caused more confusion on my part due to the word Variables.

In our documents we do not have anything like {DOCVARIABLE Bob \*MERGEFORMAT} it is more like { DOCVARIABLE Bob }. I'll to lookup the \*MERGEFORMAT.

Thanks again for your help....it's much appreciated that you went the extra distance to help me out.

 
So DOCVARIABLES are FIELDS, correct?
There is a DOCVARIABLE Field, yes. Like a FILENAME field, which points to the value of filename. Or a REF field, which points to (refers to) another field, or bookmark. Like a AUTHOR field, which points to the Author item of the BuiltinDocumentProperties. Like the FILESIZE field, which points to....

The DOCVARIABLE field points to the named document variable. It is simply a way to make a document variable (a variable within the document code) visible in the document.

faq219-2884

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top