Old*_*eon 5 vba ms-word word-vba
多年来我构建了一个vba宏,它应该更新word文档中的所有字段.
我在发布文档以供审阅之前调用此宏,以确保所有页眉和页脚等都正确.
目前 - 它看起来像这样:
Sub UpdateAllFields()
'
' UpdateAllFields Macro
'
'
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 = doc.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
If rngStory.StoryType <> wdMainTextStory Then
While Not (rngStory.NextStoryRange Is Nothing)
Set rngStory = rngStory.NextStoryRange
rngStory.Fields.Update
Wend
End If
End If
Next
For Each shp In doc.Shapes
If shp.Type <> msoPicture Then
With shp.TextFrame
If .HasText Then
shp.TextFrame.TextRange.Fields.Update
End If
End With
End If
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
' Header and footer too.
UpdateHeader
UpdateFooter
' 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
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
For Each footer In ActiveDocument.Sections(ActiveDocument.Sections.Count).Footers()
footer.Range.Fields.Update
Next
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
For Each header In ActiveDocument.Sections(ActiveDocument.Sections.Count).Headers()
header.Range.Fields.Update
Next
End If
Application.ScreenUpdating = True
End Sub
Run Code Online (Sandbox Code Playgroud)
我最近注意到它有时会遗漏文档的某些部分.今天它错过了第一页页脚2 -因为文档版本没有更新.
我已经建立了这个宏观多年和一些研究,但我并不为此感到自豪,所以如果现在有一个干净的方法,请建议完全替代.我正在使用Word 2007.
要进行测试,请创建一个Word文档并添加一个名为的自定义字段Version并为其赋值.然后{DOCPROPERTY Version \* MERGEFORMAT }在尽可能多的地方使用该字段.页眉,页脚,第一页,后续页面等.请记住使用不同的页眉/页脚制作多节文档.然后更改属性并调用宏.它目前做得相当不错,处理TOC和TOA TOF等,它似乎只是在多节文档中跳过页脚(有时).
似乎导致最多问题的具有挑战性的文档的结构如下:
它有3个部分.
第1节用于标题页和TOC,因此该部分的第一页没有页眉/页脚,但确实使用了该Version属性.后续页面具有用于TOC的罗马数字的页面编号.
第2节是文档正文,有页眉和页脚.
第3节是版权模糊,这有一个非常奇怪的标题和一个缩减的页脚.
所有页脚都包含Version自定义文档属性.
上面的代码似乎适用于所有情况,除非有时它错过了第2和第3部分的第一页脚.
Cin*_*ter 14
多年来,我用于更新文档中所有字段(单独处理的TOC等除外)的标准是Word MVP使用和推荐的标准,我将在此处复制.它来自Greg Maxey的网站:http://gregmaxey.mvps.org/word_tip_pages/word_fields.html.我在您的版本中没有看到的一件事是更新页眉/页脚中的形状(文本框)中的任何字段.
Public Sub UpdateAllFields()
Dim rngStory As Word.Range
Dim lngJunk As Long
Dim oShp As Shape
lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
For Each rngStory In ActiveDocument.StoryRanges
'Iterate through all linked stories
Do
On Error Resume Next
rngStory.Fields.Update
Select Case rngStory.StoryType
Case 6, 7, 8, 9, 10, 11
If rngStory.ShapeRange.Count > 0 Then
For Each oShp In rngStory.ShapeRange
If oShp.TextFrame.HasText Then
oShp.TextFrame.TextRange.Fields.Update
End If
Next
End If
Case Else
'Do Nothing
End Select
On Error GoTo 0
'Get next linked story (if any)
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next
End Sub
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16731 次 |
| 最近记录: |