Word

Update fields in header and footers

Ease of Use

Easy

Version tested with

2003 

Submitted by:

MOS MASTER

Description:

Programmatically update fields in document and headers and footers. 

Discussion:

Updating fields in the document is usually not so hard but updating those fields in headers and footers can be difficult. The provided procedures will help you in most cases. 

Code:

instructions for use

			

Option Explicit 'This one updates all the fields in the document: Sub UpdateALL() Dim oStory As Object Dim oToc As Object 'exit if no document is open If Documents.Count = 0 Then Exit Sub Application.ScreenUpdating = False For Each oStory In ActiveDocument.StoryRanges oStory.Fields.Update 'update fields in all stories Next oStory For Each oToc In ActiveDocument.TablesOfContents oToc.Update 'update TOC's Next oToc Application.ScreenUpdating = True End Sub 'This is the Dirty way to do it: Sub UpdateALL2() 'exit if no document is open If Documents.Count = 0 Then Exit Sub Application.ScreenUpdating = False 'changing views updates all fields! With ActiveWindow.View .Type = wdMasterView .Type = wdPrintView End With Application.ScreenUpdating = True End Sub 'Update only the fields in your footer like: Sub UpdateFooter() Dim i As Integer 'exit if no document is open If Documents.Count = 0 Then Exit Sub Application.ScreenUpdating = False 'Get page count i = ActiveDocument.BuiltInDocumentProperties(14) If i >= 1 Then 'Update fields in Footer ActiveDocument.Sections(ActiveDocument.Sections.Count) _ .Footers(1).Range.Fields.Update End If Application.ScreenUpdating = True End Sub 'Update only the fields in your footer like: Sub UpdateHeader() Dim i As Integer 'exit if no document is open If Documents.Count = 0 Then Exit Sub Application.ScreenUpdating = False 'Get page count i = ActiveDocument.BuiltInDocumentProperties(14) If i >= 1 Then 'Update fields in Header ActiveDocument.Sections(ActiveDocument.Sections.Count) _ .Headers(1).Range.Fields.Update End If Application.ScreenUpdating = True End Sub 'In some versions of Word you have to move the selection to the section you're updating Sub UpdateFooter2() Dim i As Integer 'exit if no document is open If Documents.Count = 0 Then Exit Sub Application.ScreenUpdating = False 'Get page count i = ActiveDocument.BuiltInDocumentProperties(14) If i >= 1 Then 'Update fields in Footer Selection.EndKey wdStory 'move selection to end of document ActiveDocument.Sections(ActiveDocument.Sections.Count) _ .Footers(1).Range.Fields.Update End If Application.ScreenUpdating = True End Sub

How to use:

  1. Open your Word document.
  2. Press Alt + F11 to open VBE.
  3. Insert-Module. (Insert -> module)
  4. Paste the code there in the window at right. (F7)
  5. Close VBE (Alt + Q or press the X in the top right hand corner).
  6. Save the file.
 

Test the code:

  1. From Word, press Alt + F8 to open the macro dialog box.
  2. Select one of the update procedures
  3. Click Run.
 

Sample File:

Update fields in header and footers.zip 13.39KB 

Approved by mdmackillop


This entry has been viewed 207 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express