Office中国论坛/Access中国论坛

标题: 更新文档中所有字段, 包括页眉与页脚中的字段 [打印本页]

作者: tmtony    时间: 2004-7-9 07:05
标题: 更新文档中所有字段, 包括页眉与页脚中的字段
更新文档中所有字段, 包括页眉与页脚中的字段:

作者:Bryan Carbonnell   ,Nancy Hutson Hale

Public Sub UpdateAllFields()



'---------------------------------------------------------------------------------------

' Procedure: sUpdateFields (V2)

' DateTime : 20-Dec-2001

' Updated  : 06-Nov-2002 - Update fields in Text Boxes

' Author   : Bryan Carbonnell

'            With code by Nancy Hutson Hale

' Purpose  : To update all fields in the Word Document including TOC, TOA, TOF,

'             fields in text boxes and fields in headers/footers

'---------------------------------------------------------------------------------------



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 Object 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              ' Shape Object to get Textboxes



' 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



'Loop through text boxes and update

' added by Nancy Hutson Hale - Nov 6, 2002

For Each shp In doc.Shapes

  With shp.TextFrame

    If .HasText Then

      .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




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3