Results 1 to 7 of 7
  1. #1
    Star Lounger
    Join Date
    Jan 2001
    Location
    Switzerland
    Posts
    90
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Custom Fields in Word (Word 2003)

    Folks

    A quick question... is it possible to create custom fields in VBA to be used in Word documents ?

    Thanks & regards

    alex

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

    Re: Custom Fields in Word (Word 2003)

    Not quite sure what you mean. You can create document variables in VBA:

    ActiveDocument.Variables.Add Name:="MyVariable", Value:="Woody's Lounge"

    You can refer to a document variable in a document by using a DocVariable field:

    { DocVariable MyVariable }

    (Note: you should insert a field using Insert | Field or by typing Ctrl+F9; you should not type the field braces { } yourself.)

    You can also create custom document properties interactively (through File | Properties) or in VBA:

    ActiveDocument.CustomDocumentProperties.Add Name:="MyProperty", _
    LinkToContent:=False, _
    Type:=msoPropertyTypeString, _
    Value:="Woody's Lounge"

    You can refer to a document property in the document by using a DocProperty field:

    { DocProperty MyProperty }

  3. #3
    Star Lounger
    Join Date
    Jan 2001
    Location
    Switzerland
    Posts
    90
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Custom Fields in Word (Word 2003)

    Hello

    Thanks for the pointer.

    Document variables are definitely what I was looking for... I still have a question, though: is there any way to have some code updating those variables when document fields are updated. In other words, those Word fire an event that I could trap and run my custom code to update my variables ?

    Thanks again

    Alex

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

    Re: Custom Fields in Word (Word 2003)

    I'm sorry, I don't understand. Can you explain what you mean by "updating those variables when document fields are updated"?

  5. #5
    Star Lounger
    Join Date
    Jan 2001
    Location
    Switzerland
    Posts
    90
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Custom Fields in Word (Word 2003)

    <hr>I'm sorry, I don't understand. Can you explain what you mean by "updating those variables when document fields are updated"?<hr>

    Well... as you are probably well aware document fields are automatically updated by Word on certain occasions. Typically the NUMPAGES field, which holds the total number of pages of the document, will be updated before printing.

    I was wondering if there was any way to trap this and execute some custom VBA in order to update my document variable to whatever value would be relevant at that time.

    Hope I made myself a little clearer

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

    Re: Custom Fields in Word (Word 2003)

    Fields such as DATE or NUMPAGES are dynamic by nature, Word knows how to update them.

    Document variables are static strings, Word has no idea what their content or purpose is. So you will have to write code to set new values for the document variables if appropriate. The concept "update" does not apply.

    You could write macros named FilePrint and FilePrintDefault; they would intercept the Print commands from the File menu and Standard toolbar, respectively. Or you could write an application level DocumentBeforePrint event handler, but that is rather technical. Perhaps the easiest solution is to write a macro that "updates" the values of the document variables (and perhaps print the document), and assign that macro to a toolbar button.

  7. #7
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Custom Fields in Word (Word 2003)

    You can avoid VBA to do this. If you use Custom Document Properties rather than Document Variables then you can link the contents of a bookmark to the custom document property. This then allows you to change the contents of the bookmark and have the custom doc property update automatically without bothering with VBA. The trick to doing this is to create the content in the document and bookmark it before going to File > Properties and clicking on the Custom tab.

    Hans' early response included VBA code to create these properties through code although his sample included a line turning the link to content off rather than on.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

Posting Permissions

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