HatchIT
asked on
How check the actuals docVariables in the Word file.
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
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
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
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
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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 }
It might help to display the document fields with the Alt+F9 toggle.
This would switch the display between
MyVariableValue and { DOCVARIABLE MyVariable }
ASKER
Excellent,
It the best option to me. With this I can create my code.
Thank you GrahamSkan
It the best option to me. With this I can create my code.
Thank you GrahamSkan
I would write it like this:
Open in new window