|
|
|
|
|
|
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
Sub UpdateALL()
Dim oStory As Object
Dim oToc As Object
If Documents.Count = 0 Then Exit Sub
Application.ScreenUpdating = False
For Each oStory In ActiveDocument.StoryRanges
oStory.Fields.Update
Next oStory
For Each oToc In ActiveDocument.TablesOfContents
oToc.Update
Next oToc
Application.ScreenUpdating = True
End Sub
Sub UpdateALL2()
If Documents.Count = 0 Then Exit Sub
Application.ScreenUpdating = False
With ActiveWindow.View
.Type = wdMasterView
.Type = wdPrintView
End With
Application.ScreenUpdating = True
End Sub
Sub UpdateFooter()
Dim i As Integer
If Documents.Count = 0 Then Exit Sub
Application.ScreenUpdating = False
i = ActiveDocument.BuiltInDocumentProperties(14)
If i >= 1 Then
ActiveDocument.Sections(ActiveDocument.Sections.Count) _
.Footers(1).Range.Fields.Update
End If
Application.ScreenUpdating = True
End Sub
Sub UpdateHeader()
Dim i As Integer
If Documents.Count = 0 Then Exit Sub
Application.ScreenUpdating = False
i = ActiveDocument.BuiltInDocumentProperties(14)
If i >= 1 Then
ActiveDocument.Sections(ActiveDocument.Sections.Count) _
.Headers(1).Range.Fields.Update
End If
Application.ScreenUpdating = True
End Sub
Sub UpdateFooter2()
Dim i As Integer
If Documents.Count = 0 Then Exit Sub
Application.ScreenUpdating = False
i = ActiveDocument.BuiltInDocumentProperties(14)
If i >= 1 Then
Selection.EndKey wdStory
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 207 times.
|
|