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).
--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
>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.
[quoted text clipped - 15 lines]
>> in a document-property,
>> be it custom or built-in.