Results 1 to 12 of 12
  1. #1
    2 Star Lounger
    Join Date
    Jul 2003
    Location
    Park City, Utah
    Posts
    189
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Assigning Variable Contents (Word XP VBA)

    I use a great little add-in for Word called DataPrompter which creates document property fields and variables based on user input. I want to capture this data to variables which will insert some of the same data into a new document.

    For example, one of the fields is:

    {DOCPROPERTY "z:|_AdmitDate"*MERGEFORMAT}

    How do I get the contents of this field into a variable, called strAdmitDate?

    Thanks!

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

    Re: Assigning Variable Contents (Word XP VBA)

    Custom document properties are contained in the CustomDocumentProperties collection. Example:

    strAdmitDate = ActiveDocument.CustomDocumentProperties("z:|_Admit Date").Value

  3. #3
    2 Star Lounger
    Join Date
    Jul 2003
    Location
    Park City, Utah
    Posts
    189
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Assigning Variable Contents (Word XP VBA)

    Thanks!

    Now what about going the other way? I have contents in a variable and want to send that to the document property field?

    Jeanie

  4. #4
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Assigning Variable Contents (Word XP VBA)

    To copy the value in the other directio, just reverse the two parameters
    ActiveDocument.CustomDocumentProperties("z:|_Admit Date").Value = strAdmitDate

    StuartR

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Assigning Variable Contents (Word XP VBA)

    And if the property does not exist yet, you'll need the .Add method.

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

    Re: Assigning Variable Contents (Word XP VBA)

    There is an excellent article referenced on http://www.mvps.org/word/Tutorials/Subscription.htm all about using document variables:
    Supercharged Word

  7. #7
    2 Star Lounger
    Join Date
    Jul 2003
    Location
    Park City, Utah
    Posts
    189
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Assigning Variable Contents (Word XP VBA)

    Thanks for the info! I did figure out the error problem, especially the one where it crashes if one of the document variables contains nothing. It took me 2 hours to figure that one ou5. Easily fixed, though.

    JeanieB

  8. #8
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Assigning Variable Contents (Word XP VBA)

    You don't have to use error handling to determine if a document property exists- you can go through the collection-

    Dim dp As DocumentProperty
    For Each dp In ActiveDocument.CustomDocumentProperties
    Debug.Print dp.Name
    Next
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

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

    Re: Assigning Variable Contents (Word XP VBA)

    This brings up a further question. It appears that there are two distinct collections here:
    ActiveDocument.CustomDocumentProperties that you have mentioned and
    ActiveDocument.Variables, the basis of the article I cited.

    I'm not sure which collection the OP was referring to. Both being collections, I guess they're both "searchable" in the way you suggest, and both generate the same trappable errors in the way the Romke Soldaat article suggests. But what are the differences? I think that the custom properties are at least visible (maybe also changeable) at the user interface, but the document variables are not. Any other differences?

    Alan

  10. #10
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Assigning Variable Contents (Word XP VBA)

    I think you have correctly identified the only significant difference.

    Custom document properties can be viewed and changed from the Custom tab in File > Properties. Document variables can only be viewed from the VBE, or using a DocVariable field.

    I always iterate through the collection to see if I need to Add these or just change the existing value. I create a pair of procedures with names like SetProperty and GetProperty so that the mainline code is easy to read and maintain.

    StuartR

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

    Re: Assigning Variable Contents (Word XP VBA)

    Thanks Stuart. That seems like good advice, considering the way in which errors can be generated quite "innocently". Your set & get procedures seem like a better alternative to trapping errors... certainly cleaner coding to prevent errors being generated in the first place than to design traps, when the cause is already known in advance.

    cheers

    Alan

  12. #12
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Assigning Variable Contents (Word XP VBA)

    I'd much prefer to trap foreseaable events cleanly, wherever possible.

    Using error trapping can lead to unforeseen errors being caught in the same trap.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

Posting Permissions

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