In this article, we shall focus on expounding the way to auto update all fields in a document before you closing it.
We use fields to hold data that may change later. So once we make some change to them, we need to update fields correspondingly. And unfortunately, most of time, we will have to update them manually.
Besides, the biggest problem lies at we can easily forget to update fields. To address this issue, we offer you a macro to auto update fields before closing document.
Update All Fields before Printing
You can alter Word options to set it update fields before printing with following steps:
- Firstly, click “File” tab then the “Options”.
- Next in “Word Options” box open, click “Display” on the left column.
- On the right section, go to “Printing options” part to check the “Update fields before printing” box.
- Lastly, click “OK”.
Auto Update All Fields before Closing Document
- To begin with, trigger the VBA editor in Word pressing “Alt+ F11”.
- Next click “Normal” project.
- Then on the menu bar, click “Insert”.
- And select “Class Module”.
- Next click “Properties Window” command on menu bar or hit “F4” to trigger it.
- In the window open at the left-down corner, name the newly created class module as “objWordClass”.
- On the coding area enter following codes:
Option Explicit Public WithEvents objWord As Word.Application Private Sub objWord_DocumentBeforeClose(ByVal objDoc As Document, varCancel As Boolean) Dim strButtonValue As String Application.ScreenUpdating = False Set objDoc = ActiveDocument objDoc.Save strButtonValue = MsgBox("Do you want to update all fields in this document before closing?", _ vbYesNo + vbQuestion) If strButtonValue = vbYes Then varCancel = True If objDoc.Fields.Count > 0 Then With objDoc .Fields.Update .Save .Close End With Else MsgBox ("There is no field in this document.") End If Else varCancel = False End If Application.ScreenUpdating = True End Sub
- And remember to click “Save” button.
- Now still under “Normal” project, click “Insert” again and select “Module” this time.
- Next open module and paste these codes:
Option Explicit Dim objWordClass As New objWordClass Public Sub AutoOpen() Set objWordClass.objWord = Word.Application End Sub
- Similarly, save codes.
- Next time you finishing editing a document, save it. And when you click the “Close” command, you will receive the asking box. Click “Yes” to update all fields and the document will be closed when the task is done.
Bring Dead Document back to Life
It’s inevitably to come across some problems now and then while using Word. Thus, it’s advisable to be prepared for any possible damaged doc. The best way is, of course, to get a specialized repairing tool in advance.
Author Introduction:
Vera Chen is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including repair Excel and pdf repair software products. For more information visit www.datanumen.com
1