Home All Groups Group Topic Archive Search About

Persistence of Document Variables values

Author
17 Jun 2009 3:34 AM
THendr2929
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
value
            'in the Immediate Window
            ActiveDocument.Variables.Add Name:="PersistentVariableTest",
Value:="True"
            Debug.Print "New Variable Value: " &
ActiveDocument.Variables("PersistentVariableTest")
            ActiveDocument.Save
          Else
            'If the Variable is found then print its assigned value
            'in the Immediate Window
            Debug.Print "Found Variable Value: " &
ActiveDocument.Variables("PersistentVariableTest")
    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)

Author
17 Jun 2009 12:40 PM
Nobody
Try posting to:

news://msnews.microsoft.com/microsoft.public.word.vba.general
Author
18 Jun 2009 2:19 PM
CY
Ehh, a file, the registry, a database.. the filename, white text on
the document... just those that came...
//CY