Results 1 to 5 of 5
  1. #1
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Document Variable query (2000)

    I'm getting a consistent but odd result trying to set a docvar to a boolean value:

    Public Sub test()
    On Error Resume Next
    ActiveDocument.Variables("Temp").Delete
    ActiveDocument.Variables.Add Name:="Temp", Value:=False
    MsgBox ActiveDocument.Variables("Temp").Value ' shows <font color=red>0</font color=red>
    ActiveDocument.Variables("Temp").Value = False
    MsgBox ActiveDocument.Variables("Temp").Value ' shows <font color=red>False</font color=red>
    End Sub

    According to the help file, the .Value property of a document variable object is a string (I always thought it held a variant, since you can't assign it a type).
    What's the story here? Is the "False" really a bool, or is it a string?

    Alan

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Document Variable query (2000)

    VarType returns 8 = string in both cases. It is certainly an inconsistency in the way Word assigns the value; apparently the first time False is converted to its numeric equivalent 0, and that is stored as "0", while the second time the string "False" is stored. To get the same result both times, explicitly force the conversion by assigning CStr(False) as value.

  3. #3
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Document Variable query (2000)

    Thanks Hans. I was hoping to use the value as a logical argument in an if statement. I guess this is not viable/ reliable judging by the inconsistent behaviour. Would "= 0" work in the if statement if the .Value returned a "0" string rather than numeric?

    Alan

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Document Variable query (2000)

    VBA is pretty good at converting types on the fly. Even if the contents of a document variable are "False" as a string, the test

    Debug.Print (ActiveDocument.Variables("Temp") = False)

    returns True in both cases in your macro - note that there are no quotes around False in the comparison. So you can use a document variable containing True/False as if it is a Boolean.

  5. #5
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Document Variable query (2000)

    Thanks again. This "on the fly" conversion helps explain some of the strange results I've been getting i.e. one thing indicating that a method shouldn't work, but then discovering that it actually does. The VarType function looks to be quite handy for unravelling such mysteries.

    cheers

    Alan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •