• Custom Fields in Word (Word 2003)

    Author
    Topic
    #405406

    Folks

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

    Thanks & regards

    alex

    Viewing 1 reply thread
    Author
    Replies
    • #832616

      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 }

      • #836821

        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

        • #836823

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

          • #836829

            [indent]


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


            [/indent]

            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

            • #836833

              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.

            • #836834

              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.

            • #836946

              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.

          • #836830

            [indent]


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


            [/indent]

            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

        • #836824

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

      • #836822

        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

    • #832617

      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 }

    Viewing 1 reply thread
    Reply To: Custom Fields in Word (Word 2003)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: