How to Auto Update All Fields before Closing Your Word Document

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.Auto Update All Fields before Closing Your Word Document

Update All Fields before Printing

You can alter Word options to set it update fields before printing with following steps:

  1. Firstly, click “File” tab then the “Options”.
  2. Next in “Word Options” box open, click “Display” on the left column.
  3. On the right section, go to “Printing options” part to check the “Update fields before printing” box.
  4. Lastly, click “OK”.Click "Display"->Check "Update fields before printing" Box->Click "OK"

Auto Update All Fields before Closing Document

  1. To begin with, trigger the VBA editor in Word pressing “Alt+ F11”.
  2. Next click “Normal” project.
  3. Then on the menu bar, click “Insert”.
  4. And select “Class Module”.Click "Normal"->Click "Insert"->Choose "Class Module"
  5. Next click “Properties Window” command on menu bar or hit “F4” to trigger it.
  6. In the window open at the left-down corner, name the newly created class module as “objWordClass”.
  7. 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
  1. And remember to click “Save” button.Click "Properties Window"->Name the Class Module->Paste Codes->Save Codes
  2. Now still under “Normal” project, click “Insert” again and select “Module” this time.Click "Normal"->Click "Insert"->Click "Module"
  3. Next open module and paste these codes:
Option Explicit
  Dim objWordClass As New objWordClass
  Public Sub AutoOpen()
  Set objWordClass.objWord = Word.Application
End Sub
  1. Similarly, save codes.
  2. 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.Click "Close"->Click "Yes" or "No"

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

Leave a Reply

Your email address will not be published.