Dawn Crosier was telling us:
Dawn Crosier nous racontait que :
I would store the information in Document Variables if you want the
data available for repeat running.
Here is some sample code which will check to see whether a variable
exists, if not it will help you to create one, and add a value to it.
Just thought I'd mention to those interested with "short" code that you do
not need to check for existence if you want to create a document variable:
ActiveDocument.Variables("varClientName").Value = "test"
will create it if it does not exist, and
ActiveDocument.Variables("varClientName").Value = ""
will effectively delete the variable.
You still have to check if it exists if you want to read the variable
though.
ActiveDocument.Variables("varClientName").Value = ""
MsgBox ActiveDocument.Variables("varClientName").Value
will generate an error.
'Test to see whether document variables exist if not add then
If DocVarExists("varClientName") Then
ActiveDocument.Variables("varClientName").Value = "test"
Else
ActiveDocument.Variables.Add Name:="varClientName",
Value:="test"
End If
'Function used to check to make sure the Document Variable exists
because
'If you try to add it, and it does not exist - an error will be
returned.
'Same is true if you try to set its value and it does not exist.
Function DocVarExists(strVarName As String) As Boolean
Dim varDocVars As Variant
DocVarExists = False
For Each varDocVars In ActiveDocument.Variables
If varDocVars.Name = strVarName Then DocVarExists = True
Next varDocVars
End Function
To retrieve a Document Variable for use
= ActiveDocument.Variables("varVersion").Value
To update a Bookmark based on a result from code
ActiveDocument.Bookmarks("DateOfLetter").Range = _
ActiveDocument.Variables("varVersion").Value
I hope this helps.
--
Dawn Crosier
Microsoft MVP
"Education Lasts a Lifetime"
This message is posted to a newsgroup. Please post replies and
questions to the newsgroup so that others can learn as well.
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site:
http://www.word.mvps.org