Solved

How check the actuals docVariables in the Word file.

Posted on 2011-02-14
8
1,279 Views
Last Modified: 2012-05-11
I'm programing  a Word Template with a UserForm and I need check if all the docVatiables created in the teplate exist in the new file yet.

I created the attached code to list all Fields in the file and it is working but if I delete some docVariables in the file, this code find the same list.

I'm searching some code in the web but can't found.

Someone know how check the real list of docVariables?

Regards,
Claudio


Sub ListFields()
    ActiveDocument.ResetFormFields
    For Each Field In ActiveDocument.Variables
        ListField = ListField & vbCrLf & Field.Name
    Next
    MsgBox ListField
End Sub

Open in new window

0
Comment
Question by:HatchIT
  • 6
  • 2
8 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 34891083
As it stands your code should work, but I think that you might be getting document variables and fields confused.

I would write it like this:
Sub DocVariables()
    Dim var As Variable
    Dim strMessage As String
    
    For Each var In ActiveDocument.Variables
        strMessage = strMessage & vbCrLf & strMessage
    Next var
    MsgBox strMessage
End Sub

Open in new window

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 34891174
You could list the fields like this
Sub ListFields()
    Dim fld As Field
    Dim strMessage As String
    
    For Each fld In ActiveDocument.Fields
        strMessage = "Result: " & fld.Result & ", Type: " & fld.Type & ", Start position: " & fld.Code.Start & vbCrLf & strMessage
    Next fld
    MsgBox strMessage
End Sub

Open in new window

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 34891206
Sorry. The first one was wrong. Try this for the variables:
Sub DocVariables()
    Dim var As Variable
    Dim strMessage As String
    
    For Each var In ActiveDocument.Variables
        strMessage = "Name: " & var.Name & ", Value: " & var.Value & vbCrLf & strMessage
    Next var
    MsgBox strMessage
End Sub

Open in new window

0
 
LVL 1

Author Comment

by:HatchIT
ID: 34908139
GrahamSkan

My problem isn't list fields or variables, the problem is the next:

If in a file I did create 10 fields then save/close/open and then I delete one, more or all fields in the file, ActiveDocument.Variables keep this 10 fields in the memory although the file was save/close some times. I concluded that this is because was Fields have mantained in the "cache file information" or any site (internal head) and I need to check the actual fields in the file.

Use the Field.Delete option isn't a solutio because this clear informatión in the all fields, and I only need know if by accident anyone delete any field.

Do you have any idea to check this?

Thank you
Claudio
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 76

Accepted Solution

by:
GrahamSkan earned 250 total points
ID: 34911234
Are you saying that you have some docVariable fields in a document, and when you delete them, they are still there?

If so, how do you delete them?

You could improve the list method by using this code.
 
Sub ListDocVarFields()
    Dim fld As Field
    Dim strMessage As String
    Dim p As Integer
    For Each fld In ActiveDocument.Fields
        If fld.Type = wdFieldDocVariable Then
            p = InStr(1, fld.Code.Text, "docvariable", vbTextCompare)
            strMessage = "Name: " & Trim$(Mid$(fld.Code.Text, p + 11)) & ", result: " & fld.Result & vbCrLf & strMessage
        End If
    Next fld
    MsgBox strMessage
End Sub

Open in new window

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 34911275
This would delete the first docVariable field that displays the value of the Variable called "MyVariable".
Sub DeleteDocVarField()
    Dim fld As Field
    Dim strMessage As String
    Dim p As Integer
    For Each fld In ActiveDocument.Fields
        If fld.Type = wdFieldDocVariable Then
            p = InStr(1, fld.Code.Text, "docvariable", vbTextCompare)
            If Trim$(Mid$(fld.Code.Text, p + 11)) = "MyVariable" Then
                fld.Delete
                Exit For
            End If
        End If
    Next fld
End Sub

Open in new window

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 34912110
To answer your point about there being some sort of cache, the answer is 'not in my experience'. A saved and closed document should not not find deleted data when re-opened. If it does, then there is a fault with your document or your Word application.

It might help to display the document fields with the Alt+F9 toggle.

This would switch the display between

MyVariableValue   and   { DOCVARIABLE MyVariable }
0
 
LVL 1

Author Closing Comment

by:HatchIT
ID: 34925405
Excellent,

It the best option to me. With this I can create my code.

Thank you GrahamSkan
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Like many others, we try and discourage users from printing documents unnecessarily and instead send or share them electronically. However, this doesn't always work and documents are still printed. With this simple solution, if the user tries to …
Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now