How to use VBA to update fields in a Word document

2 years ago 268

Fields that don't update automatically airs a occupation with galore users due to the fact that they don't retrieve to update them manually. When you request this functionality, usage this VBA procedure.

asian-student-surf-internet-for-job-hunting-freelancer-blog-writer-picture-id1132736227.jpg

Image: insjoy, Getty Images/iStockPhoto

The nonfiction How to update a leafage fig notation successful a Word document uses fields to update cross-references successful a Microsoft Word document. The occupation is that the fields successful usage won't update automatically, truthful the idiosyncratic indispensable retrieve to bash so. That's not perfect due to the fact that it's a task that's casual to forget. In this article, I'll amusement you a built-in mounting that updates the fields earlier printing, and past I'll amusement you a VBA solution.

SEE: 83 Excel tips each idiosyncratic should master (TechRepublic)

I'm utilizing Microsoft 365 connected a Windows 10 64-bit system, but you tin usage earlier versions. Word Online won't enactment the solutions successful this article. For speedy entree to the VBA code, you tin download the objection .docm, .doc and .cls files.

How to update erstwhile printing successful Word

To update an idiosyncratic field, click connected it and property F9 oregon right-click and prime Update Field. To update each references successful a document, property Ctrl + A, past property F9. Even if you lone person 1 oregon a few, it's hard to retrieve to manually update those fields. Instead, you tin acceptable Word to ever update fields earlier printing your papers arsenic follows:

  1. Click the File paper and take Options oregon More and past Options successful the near pane.
  2. Choose Display successful the near pane.
  3. In the Printing section, cheque the Update Fields Before Printing enactment (Figure A).
  4. Click OK.

Figure A

wordupdatefieldmacro-a.jpg

  Choose this enactment to update fields erstwhile printing.

As you mightiness suspect, this enactment is limited. First, if you're sending the papers by email you won't beryllium printing, truthful this enactment is useless. In addition, you mightiness not announcement that a tract doesn't update if a bookmark oregon notation is breached due to the fact that you won't beryllium viewing the papers earlier updating erstwhile printing.

This enactment has its place, but let's look astatine a codification solution that's much dependable.

How to update with VBA successful Word

The VBA bid to update fields

ThisDocument.Fields.Update

is casual to implement. Listing A shows however casual it is to update each fields erstwhile closing a document.

Listing A

Private Sub Document_Close()

'Update each fields erstwhile closing document.

    ThisDocument.Fields.Update

End Sub

This process uses the Document object's Close event. That means, closing the papers triggers the lawsuit that updates each fields. It's slick and easy. The lone existent occupation is deciding what lawsuit to use, and Close makes the astir consciousness to me. You tin usage the Open event, but that won't update fields aft you modify the document. 

You could trigger some the Open and Close events to screen astir situations. In addition, cheque the enactment reviewed astatine the opening of this nonfiction to screen astir each imaginable scenario:

  • If the idiosyncratic prints the document, the fields are updated first.
  • If the idiosyncratic makes modifications and closes the papers earlier emailing, the fields are updated first.
  • If the idiosyncratic opens the papers and prints without making modifications, the fields are updated first.

There is 1 anticipation that isn't covered: The idiosyncratic opens the file, makes modifications, and past sends the record without closing the document, which isn't apt due to the fact that astir of america would adjacent and prevention the papers earlier sending it.

SEE: Windows 10: Lists of vocal commands for code designation and dictation (free PDF) (TechRepublic)

I've added the process successful Listing A to the downloadable files. I'm utilizing the demonstration files utilized successful the linked nonfiction successful the introduction. You tin enactment with immoderate Word document, but you'll privation to adhd a tract that doesn't update automatically to the assemblage of the document. There are 3 pages. On leafage 2 is simply a substance reference. On leafage 3, a cross-reference tract returns the leafage fig of the substance notation connected leafage 2. If you participate a leafage betwixt pages 1 and 2, the substance notation moves to leafage 3 and the cross-reference moves to leafage 4. However, the cross-reference volition inactive instrumentality leafage 2 due to the fact that it won't update automatically. 

If you determine to participate the process manually, beryllium definite to prevention the workbook arsenic a macro-enabled record if you're utilizing a ribbon mentation oregon the process won't run. If you're utilizing a paper version, you tin skip this step. To adhd the procedure, property Alt + F11 to unfastened the Visual Basic Editor. In the Project Explorer to the left, prime ThisDocument. You tin participate the codification manually oregon import the downloadable .cls file. In addition, the process is successful the downloadable .docm and .doc files. If you participate the codification manually, don't paste from this web page. Instead, transcript the codification into a substance exertion and past paste that codification into the ThisDocument module. Doing truthful volition region immoderate phantom web characters that mightiness different origin errors.  

To usage the process simply adjacent your document. But first, if you're utilizing a objection file, adhd a leafage to the objection .docm oregon .doc record betwixt pages 1 and leafage 2 to determination the substance notation connected leafage 2 to leafage 3. The cross-reference tract that was connected leafage 3 is present connected leafage 4, but it won't update automatically. When prompted to prevention erstwhile you adjacent the file, click Save. When you reopen the document, immoderate tract that needs to beryllium updated volition show the close result. To larn much astir this objection file's fields and its purpose, work the linked nonfiction successful the introduction.

If you find this a spot excessively confusing, you don't request to usage the objection file. You tin effort this with immoderate Word papers that contains fields that don't update automatically.

This elemental process has 1 limitation, which doesn't interaction our circumstantial example: It updates fields lone successful the main communicative of the document. It won't update fields successful headers, footers, footnotes oregon endnotes. That's not an contented for america due to the fact that Word would update the {PageRef} tract (the tract we're using) if it were successful the header oregon footer. 

The process in Listing A is simply a bully spot to start, but arsenic mentioned it has limitations. In a aboriginal article, I'll amusement you however to usurp the Save bid to update fields erstwhile you prevention the file. In addition, you tin look guardant to an nonfiction that updates fields beyond the main story. 

Microsoft Weekly Newsletter

Be your company's Microsoft insider by speechmaking these Windows and Office tips, tricks, and cheat sheets. Delivered Mondays and Wednesdays

Sign up today

Also see

Read Entire Article