Results 1 to 3 of 3
  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 Types (2000 sp-3/ 98SE)

    Some simple testing indicates that the VarType of a document variable is always 8 (vbString), regardless of how it is initialized. For instance:
    <code>
    ActiveDocument.Variables.Add Name:="intVar", Value:=5
    ActiveDocument.Variables.Add Name:="strVar", Value:="A string"
    ActiveDocument.Variables.Add Name:="sngVar", Value:=3.14159
    ActiveDocument.Variables.Add Name:="blnVar", Value:=True
    </code>
    all show a String VarType for both the variable itself, as well as its .Value property. In essence, the original intended data type is lost - everything becomes a String.

    Subsequent manipulation and/ or testing of such variables appears to recast them as required, to the types they were originally initialized as. For instance, blnVar can be treated as a boolean. Can such implicit conversion be relied upon in all such situations?

    If not, then it may be necessary to explicitly coerce an appropriate data type conversion. Problem is, the original intended data typing is not accessible to the programmer after the fact. For instance, there's no way of knowing whether sngVar was intended to be of data type Single or String. This creates a problem for operations involving iterating the collection. Other than using a consistent prefixed naming system, like the above, is there any way to "retrieve" this original intent? I suspect not, but it's always worth an ask!

    Alan

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

    Re: Document Variable Types (2000 sp-3/ 98SE)

    You are correct that document variables are stored as strings, so there is no intrinsic way to know what kind of value you assigned. VBA is very flexible in recasting variables, if you assign the value of a document variable to a Boolean or Single, it will perform the necessary conversion.

    If you need to know the value type, you can use a custom document property. The DocumentProperty object has a property Type, which can be msoPropertyTypeBoolean, msoPropertyTypeDate, msoPropertyTypeFloat, msoPropertyTypeNumber or msoPropertyTypeString.

    PS Congratulations on becoming a GoldLounger! <img src=/S/clapping.gif border=0 alt=clapping width=19 height=23>

  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 Types (2000 sp-3/ 98SE)

    Thanks Hans (and for the congrats - I didn't notice <img src=/S/blush.gif border=0 alt=blush width=15 height=15>). I didn't realize that custom document properties carried a type with them. Trouble being they're exposed to the interface user. I really must give serious consideration to the "improved" docvar class I've been fiddling with for some time. For my current purpose, I guess I'll stick with systematic prefixes for variable names.

    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
  •