|
|
|
|
|
|
|
|
|
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:
|
- Open your Word document.
- Press Alt + F11 to open VBE.
- Insert-Module. (Insert -> module)
- Paste the code there in the window at right. (F7)
- Close VBE (Alt + Q or press the X in the top right hand corner).
- Save the file.
|
|
Test the code:
|
- From Word, press Alt + F8 to open the macro dialog box.
- Select one of the update procedures
- Click Run.
|
|
Sample File:
|
Update fields in header and footers.zip 13.39KB
|
|
Approved by mdmackillop
|
|
This entry has been viewed 159 times.
|
|
|