Detecting the Existance of a Document Variable

  • Thread starter Montana DOJ Help Desk
  • Start date
M

Montana DOJ Help Desk

Word 2000

I have a set of macros that rely on a document variable. I'm trying to
write a routine that will check the existing document variables to make sure
that all the required document variables exist. However, the Exists method
does not appear to be available when working with document variables.

I suppose that I could use the Add method in conjunction with On Error
Resume Next. If the document variable exists, and error will occur but the
code will continue on, and if it doesn't exist, it will be created. But it
seems like there should be a better way to do it.

How can I check for the existence of a document variable?

-- Tom

State of Montana
Department of Justice Help Desk

"Making the world a safer place."
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Montana DOJ Help Desk > écrivait :
In this message, < Montana DOJ Help Desk > wrote:

|| Word 2000
||
|| I have a set of macros that rely on a document variable. I'm trying to
|| write a routine that will check the existing document variables to make
sure
|| that all the required document variables exist. However, the Exists
method
|| does not appear to be available when working with document variables.
||
|| I suppose that I could use the Add method in conjunction with On Error
|| Resume Next. If the document variable exists, and error will occur but
the
|| code will continue on, and if it doesn't exist, it will be created. But
it
|| seems like there should be a better way to do it.
||
|| How can I check for the existence of a document variable?

It depends what you are trying to do.
If you just want to store a value, then

ActiveDocument.Variables("MyTest").Value = "Variable text"

will create the variable "MyTest" if it does not exist. No need to check.

OTOH, if you want to read a value, then an error will be thrown if it does
not exist.
So, you can check for existence first by doing something like:

'_______________________________________
Dim MyVar As Variable
Dim VarIndex As Long
Const VarName As String = "MyVariable"

VarIndex = 0

For Each MyVar In ActiveDocument.Variables
With MyVar
If .Name = VarName Then
VarIndex = .Index
Exit For
End If
End With
Next MyVar

If VarIndex <> 0 Then
MsgBox ActiveDocument.Variables(VarIndex).Value
Else
ActiveDocument.Variables(VarName).Value = "New content"
End If
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
M

Montana DOJ Help Desk

Thanks for the help! The document variable that I'm setting is a Boolean
value, and there's only one way it can be set to True, and just one way it
can be set to False, so for my purposes, using the command that you provided
for storing the value will suffice.

-- Tom

State of Montana
Department of Justice Help Desk

"Making the world a safer place."
 
P

Pete Bennett

It's even easier to write a little function that does the check for you....

This uses error trapping, but because it's self-contained, it won't affect
any other error traps you might have.

Function DoesVarExist (aDoc as Document, sName as String) as Boolean
Dim sValue as String

DoesVarExist =True
On Error Goto lNotExist
sValue=aDoc.Variables (sName).Value
On Error Goto 0
Exit Function
lNotExist:
On Error Goto 0
DoesVarExist =True
End Function
 

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