Tim创建了一个简单的宏来更新模板中的所有字段。宏包括一个选择整个文档的步骤,就像按Ctrl + A一样。此步骤不会选择页眉和页脚,Tim担心在这些位置可能会有字段要更新。他想知道在选择全部时是否存在一种包含页眉和页脚的方法,或者他的宏是否可以通过其他方式更新页眉或页脚区域中的字段。

简单的答案是,您需要修改宏代码,以使其在可能存在字段的所有不同Word区域中显示。使用“全选”方法的问题在于它仅选择主文档中的文本。页眉,页脚和其他许多元素都保留在各自独立的层中,这使它们无法与文档的所有文本一起被选择。

如果您只想更新正文中的字段以及页眉和页脚中的字段,则应使用以下技术:

Sub MyUpdateFields1()

ActiveDocument.StoryRanges(wdMainTextStory).Fields.Update     ActiveDocument.StoryRanges(wdPrimaryFooterStory).Fields.Update     ActiveDocument.StoryRanges(wdPrimaryHeaderStory).Fields.Update End Sub

请注意,文档的不同层称为“故事”,并保留在它们自己的集合中。尽管这个简单的宏非常快速地更新了包含主要文本,页脚和页眉的故事中的字段,但还有其他故事(层)

可能需要更新的字段。下面的宏浏览每个故事,无论其类型如何,并进行必要的更新:

Sub MyUpdateFields2()

Dim story As Word.Range

For Each story In ActiveDocument.StoryRanges         Do             story.Fields.Update             ' Check linked stories as linked stories are not independent             Set story = story.NextStoryRange         Loop Until (story Is Nothing)

Next End Sub

但是,如果您确实想做到完整,则不仅仅需要遍历不同的故事层。例如,可能文本框中包含某些字段需要更新。需要一个更全面的宏来处理字段可能位于的所有其他这些地方。在下面的示例中,请注意可以检查字段的文档的许多不同元素。

Sub MyUpdateFields3()

Dim doc As Document ' Pointer to Active Document     Dim wnd As Window ' Pointer to Document's Window     Dim lngMain As Long ' Main Pane Type Holder     Dim lngSplit As Long ' Split Type Holder     Dim lngActPane As Long ' ActivePane Number     Dim rngStory As Range ' Range Objwct for Looping through Stories     Dim TOC As TableOfContents ' Table of Contents Object     Dim TOA As TableOfAuthorities 'Table of Authorities Object     Dim TOF As TableOfFigures 'Table of Figures Object     Dim shp As Shape

' Set Objects     Set doc = ActiveDocument     Set wnd = ActiveDocument.ActiveWindow

' get Active Pane Number     lngActPane = wnd.ActivePane.Index

' Hold View Type of Main pane     lngMain = wnd.Panes(1).View.Type

' Hold SplitSpecial     lngSplit = wnd.View.SplitSpecial

' Get Rid of any split     wnd.View.SplitSpecial = wdPaneNone

' Set View to Normal     wnd.View.Type = wdNormalView

' Loop through each story in doc to update     For Each rngStory In doc.StoryRanges         If rngStory.StoryType = wdCommentsStory Then             Application.DisplayAlerts = wdAlertsNone             ' Update fields             rngStory.Fields.Update             Application.DisplayAlerts = wdAlertsAll         Else            ' Update fields            rngStory.Fields.Update         End If     Next

For Each shp In doc.Shapes         With shp.TextFrame             If .HasText Then                 shp.TextFrame.TextRange.Fields.Update             End If         End With     Next

' Loop through TOC and update     For Each TOC In doc.TablesOfContents         TOC.Update     Next

' Loop through TOA and update     For Each TOA In doc.TablesOfAuthorities         TOA.Update     Next

' Loop through TOF and update     For Each TOF In doc.TablesOfFigures         TOF.Update     Next

' Return Split to original state     wnd.View.SplitSpecial = lngSplit

' Return main pane to original state     wnd.Panes(1).View.Type = lngMain

' Active proper pane     wnd.Panes(lngActPane).Activate

' Close and release all pointers     Set wnd = Nothing     Set doc = Nothing End Sub

_WordTips_是您进行经济有效的Microsoft Word培训的来源。

(Microsoft Word是世界上最流行的文字处理软件。)本技巧(522)适用于Microsoft Word 97、2000、2002和2003。您可以为Word(Word 2007)的功能区界面找到此技巧的版本。和更高版本)在这里: