Hi

How can I check if the value of a variable has been set? Right now if check
the value of my variable like this;
ActiveDocument.Variables.Item("UniqueID").Value, I get an "Object has been
deleted" error.

Thanks

Regards

Re: Variable by Thomas

Thomas
Sat Jan 03 16:15:11 CST 2004

"John" <john@nospam.infovis.co.uk> wrote in message
news:e8ci8Yj0DHA.2448@TK2MSFTNGP12.phx.gbl...
> Hi
>
> How can I check if the value of a variable has been set? Right now if
check
> the value of my variable like this;
> ActiveDocument.Variables.Item("UniqueID").Value, I get an "Object has been
> deleted" error.
>
> Thanks
>
> Regards

Basically, you need to trap that error or look through all the variables in
the collection. Here is some code that should help you out. You can now do
this:

If GetVariable(ActiveDocument, "UniqueID") = "12345" Then ......


Public Sub SetVariable(oDocument As Word.Document, sName As String, sValue
As String)

On Error GoTo ErrorHandler

Dim oVariable As Word.Variable

Set oVariable = LocateVariable(oDocument, sName)

If Not oVariable Is Nothing Then

oVariable.Value = sValue

Else

oDocument.Variables.Add sName, sValue

End If

Exit Sub

ErrorHandler:

' Maybe raise an error here...

End Sub

Public Function GetVariable(oDocument As Word.Document, sName As String) As
String

On Error GoTo ErrorHandler

Dim oVariable As Word.Variable

Set oVariable = LocateVariable(oDocument, sName)

If Not oVariable Is Nothing Then

GetVariable = oVariable.Value

Else

GetVariable = ""

End If

Exit Function

ErrorHandler:

GetVariable = ""

End Function

Public Function LocateVariable(oDocument As Word.Document, sName As String)
As Word.Variable

On Error GoTo ErrorHandler

Dim oVariable As Word.Variable

For Each oVariable In oDocument.Variables

If StrComp(oVariable.Name, sName, vbTextCompare) = 0 Then

Set LocateVariable = oVariable

Exit Function

End If

Next

Set LocateVariable = Nothing

Exit Function

ErrorHandler:

Set LocateVariable = Nothing

End Function



Variable by anonymous

anonymous
Mon Jan 05 17:07:05 CST 2004


Cycle through the variable collection looking for the name.

Private Function VariableExist(VariableName As String) As
Boolean
Dim oDocVariable As Variable

For Each oDocVariable In ActiveDocument.Variables
If oDocVariable.Name = VariableName Then
VariableExist = True
End If
Next oDocVariable

End Function


Mark Baird

>-----Original Message-----
>Hi
>
>How can I check if the value of a variable has been set?
Right now if check
>the value of my variable like this;
>ActiveDocument.Variables.Item("UniqueID").Value, I get
an "Object has been
>deleted" error.
>
>Thanks
>
>Regards
>
>
>.
>