Remembering values between instances

C

Cory

My problem is as follows:
I am creating a form template that first asks the user to personalize
the form (since it hase several inputs that will never change for that user)
and then instructs the user to save the resulting document as a template.
But I want that template to run a seperate autonew. If I supplied an
If...Then statement at the begining I could just keep the Autonew that I have
and let the user Personalize then have a variable set and bypass the rest of
the instructions. When they save the template and open it the next time, the
value could be read executing the Then portion and bypassing the
personalizing bit.


So... How do you set a variable value in the code that will be read the
next time the document is opened? As far as I am aware, the variables are
not persistent. Of course keep in mind I only know enough about this to get
myself in trouble.


Cory
 
H

Helmut Weber

Hi Cory,
So... How do you set a variable value in the code that will be read the
next time the document is opened? As far as I am aware, the variables are
not persistent. Of course keep in mind I only know enough about this to get
myself in trouble.

you got to store the values somewhere,
in the registry, in a txt-file,
in a document-variable,
in a document-property,
be it custom or built-in.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
C

Cory

Alright, since I (and the other users) am/are on a regular user account, the
registry is out of the question, as far as usability, the text file is too.
How do you set a document variable or property as the value, and what are the
pros/cons of each?

Cory
 
J

Jay Freedman

Document variables and document properties are very similar. Both are
stored as part of the document's file, so they stick around through
closing and reopening the document and/or Word. There are three main
differences:

- A document variable can be created or altered only by VBA code,
while a document property can be created or altered by the user in the
Custom tab of the File > Properties dialog.

- A document variable is always stored as a string, while a document
property can be given a data type of string, number (integer), float,
date, or boolean (true/false). In practice, the conversions between
strings and the other types are mostly automatic.

- Setting a string-type document property to an empty string ("") just
makes it an empty string, while setting a document variable to an
empty string deletes the variable (so trying to get its value after
that results in an error).

To create a document variable, you can just assign a value to a
previously unused name like this:

Sub CreateVariable()
Dim myString As String
myString = "Hello World"
ActiveDocument.Variables("NameOfVariable") = myString
End Sub

You can use the ActiveDocument.Variables.Add method, but it doesn't do
anything this simple assignment doesn't do. Besides, if you try to use
the .Add method with a variable that already exists, you get an error;
but the simple assignment just changes the value.

To read back its value, you can insert a DocVariable field in the
document:
{DocVariable NameOfVariable}
or you can write code that uses its .Value member:

Sub ReadVariable()
MsgBox ActiveDocument.Variables("NameOfVariable").Value
End Sub


To create a document property, you must use the .Add method of the
CustomDocumentProperties collection:

Sub CreateProperty()
Dim myString As String
myString = "Hello World"
ActiveDocument.CustomDocumentProperties.Add _
Name:="NameOfProperty", _
LinkToContent:=False, _
Value:=myString, _
Type:=msoPropertyTypeString
End Sub

(To store one of the other data types, change the Type parameter.) The
kind of assignment that works to create a new document variable
doesn't work for document properties. :-( But you can change the
value of an existing property by assigning a new .Value to it.

To read back its value, you can insert a DocProperty field in the
document:
{DocProperty NameOfProperty}
or you can write code that uses its .Value member:

Sub ReadProperty()
MsgBox ActiveDocument _
.CustomDocumentProperties("NameOfProperty").Value
End Sub

There are a few other differences, but they're more esoteric -- for
example, I think custom document properties can be read without
opening the document by using dsofile.dll (see
http://www.word.mvps.org/FAQs/MacrosVBA/DSOFile.htm).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top