Persistence of Document Variables values 
Author Message
 Persistence of Document Variables values

I need to store a value in a Word 2007 file (any format - DOCX, DOCM, DOTX,
DOTM) that I can access between sessions.  Using the command
"ActiveDocument.Variables.Add", I add a variable to a DOCX file and assign it
a string value.  When I save the DOCX file, close and re-open it, when I try
to retrieve the variable value, I get the following error message:

     Run-time error '5825':
     Object has been deleted.

If I do the same for a DOCM file, I don't get an error message, but the
value returned does not match the value I stored - it usually consists of
question marks and random characters, and the length does not always match
the length of the string I had originally stored.

I tested this problem with the following code using a DOCM file:

    Sub CreateDocumentVariable()

        'Check all Document Variable names for desired Variable
        'If found, assign its Index value to "Num" and print its Name
        'in the Immediate Window
        For Each aVar In ActiveDocument.Variables
            If aVar.Name = "PersistentVariableTest" Then Num = aVar.Index
            Debug.Print "Variable Name: " & aVar.Name
        Next aVar

        If Num = 0 Then
            'If the Variable is not found then add it and print its assigned
            'in the Immediate Window
            ActiveDocument.Variables.Add Name:="PersistentVariableTest",
            Debug.Print "New Variable Value: " &
            'If the Variable is found then print its assigned value
            'in the Immediate Window
            Debug.Print "Found Variable Value: " &
        End If
    End Sub

My results (stepping through the code as it executed):
1.  The first time the code was run, it skipped past the For Each...Next
loop because the Document collection was empty.  It entered the If statement
(which evaluated to True) and created the Variable successfully.  The
Immediate window displayed "New Variable Value: True".
2.  The file was closed and then reopened.
3.  The second time the code was run, it found the desired Variable in the
Variables collection.  The Immediate window displayed "Variable Name:
PersistentVariableTest".  It entered the If statement (which evaluated to
False) and jumped to the Else part.  The Immediate window displayed Found
Variable Value: ??????????????A?".

My question is in regards to the persistence of Document Variables when a
file is closed and then re-opened.  In a DOCX file, it seems that the
Document Variable is not retained at all.  In a DOCM file, the Variable still
exists but the value is lost.

If I need to retain a specific value between file sessions, what then is the
best way to do this?  Building Blocks?  Document Properties?  (I am trying to
store values that the users don't need to see.)

My version information:
     Microsoft Office Word 2007 (12.0.6504.5000) SP1 MSO (12.0.6320.5000)
     Microsoft Visual Basic 6.5 Version 1024 (Forms3: 12.0.6211.100)

Sun, 04 Dec 2011 11:34:02 GMT  
 Persistence of Document Variables values
Try posting to:

Sun, 04 Dec 2011 20:40:25 GMT  
 Persistence of Document Variables values
Ehh, a file, the registry, a database.. the filename, white text on
the document... just those that came...

Mon, 05 Dec 2011 22:19:02 GMT  
 [ 3 post ] 

 Relevant Pages 

1. Document variable with value that changes throughout the document

2. How do I place a value of variable within a document

3. Create an AutoTextEntry with it's value set to a document variable

4. Retrieving a single Excel value into a Word document variable

5. Persistence of variables

6. Variable Persistence in VBScript

7. The value of a variable (String type) is the name of a variable

8. Variable Name to Variable value.

9. Create a Variable from the value of another variable

10. Getting data values to CR from variable arrays and stand alone variables in VB

11. Variable values as variable names

12. Assigning the value to the value of a variable


Powered by phpBB® Forum Software