gpt4 book ai didi

vba - 用于更新 Word 文档中所有字段的宏

转载 作者:行者123 更新时间:2023-12-02 07:40:53 24 4
gpt4 key购买 nike

多年来,我构建了一个 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

我最近注意到它有时会遗漏文档的某些部分。今天它错过了首页页脚 - 第 2 部分-(文档版本未更新)。

我已经建立了这个宏多年并进行了几次研究,但我并不为此感到自豪,所以如果现在有一种干净的方法来实现它,请建议一个完整的替代品。我使用的是 Word 2007。

要进行测试,请创建一个 Word 文档并添加一个名为 Version 的自定义字段并为其指定一个值。然后在尽可能多的地方使用该字段{DOCPROPERTY Version\* MERGEFORMAT }。页眉、页脚、第一页、后续页等等。请记住制作具有不同页眉/页脚的多节文档。然后更改属性并调用宏。它目前做得相当好,处理 TOC、TOA 和 TOF 等,例如,它似乎只是在多部分文档中跳过页脚(有时)。

编辑

似乎引起最多问题的具有挑战性的文档的结构如下:

它有 3 个部分。

  1. 第 1 部分用于标题页和目录,因此该部分的第一页没有页眉/页脚,但使用了 Version 属性。后续页面的目录页码采用罗马数字。

  2. 第 2 部分用于文档正文,包含页眉和页脚。

  3. 第 3 部分是版权简介,它有一个非常奇怪的页眉和一个精简的页脚。

所有页脚都包含Version自定义文档属性。

我上面的代码似乎在所有情况下都有效,除了有时它会错过第 2 节和第 3 节的首页页脚。

最佳答案

多年来,我用于更新文档中所有字段(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

关于vba - 用于更新 Word 文档中所有字段的宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33733113/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com