Folks
A quick question… is it possible to create custom fields in VBA to be used in Word documents ?
Thanks & regards
alex
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » 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 }
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
[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
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.
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.
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.
[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
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
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 }
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.
Notifications