【问题标题】:VBA Word macro not working as expected with field results in documentVBA Word 宏无法按预期与文档中的字段结果一起工作
【发布时间】:2018-04-27 00:00:09
【问题描述】:

我有一个 word 文档(报告),在该文档中,我正在导入许多具有如下字段的文本文件:

{INCLUDETEXT "C:\\PATH\\TOXMLFILES\\Request.xml" \*CHARFORMAT}

我还在打开文档时使用宏更新所有这些字段...

Sub AutoOpen()
With Options
    .UpdateFieldsAtPrint = True
    .UpdateLinksAtPrint = True
End With
ActiveDocument.Fields.Update
End Sub

现在我需要在 <faultstring></faultstring> 标签之间突出显示那些导入的 XML 的文本(在 IncludeText 字段中)

这是我在 stackoverflow 上获得的代码,用于突出显示文本(使其变为粗体)

Sub BoldBetweenQuotes()
    ' base for a quotes finding macro
    Dim blnSearchAgain As Boolean
    Dim blnFindStart As Boolean
    Dim blnFindEnd As Boolean
    Dim rngFind As word.Range
    Dim rngFindStart As word.Range
    Dim rngFindEnd As word.Range

    Set rngFind = ActiveDocument.content
    Set rngFindStart = rngFind.Duplicate
    Do
        ' set up find of first of quote pair
        With rngFindStart.Find
            .ClearFormatting
            .Text = "<faultstring>"
            .Replacement.Text = ""
            .Forward = True
            .wrap = wdFindStop
            blnFindStart = .Execute
        End With
        If blnFindStart Then
            rngFindStart.Collapse wdCollapseEnd
            Set rngFindEnd = rngFindStart.Duplicate
            rngFindEnd.Find.Text = "</faultstring>"
            blnFindEnd = rngFindEnd.Find.Execute
            If blnFindEnd Then
                rngFindStart.End = rngFindEnd.Start
                ' make it bold
                rngFindStart.Font.Bold = True
                rngFindStart.Start = rngFindEnd.End
                rngFindStart.End = rngFind.End
                blnSearchAgain = True
            Else
                blnSearchAgain = False
            End If
        Else
            blnSearchAgain = False
        End If
    Loop While blnSearchAgain = True
End Sub

问题是,当我在 Word 文档(带有 IncludeText 字段)中运行宏时,它会不断循环并仅在故障字符串标签之间首次出现文本时加粗。当我在带有一些随机文本和 faultrstring 标签的新 Word 文档中运行它时,它运行良好......

编辑:事实证明,问题出在IncludeText 字段内的故障字符串标签。打开文档并更新字段后,我需要将字段转换为静态文本。我该怎么做?

【问题讨论】:

  • 如果您不取消链接 INCLUDETEXT 字段,则 标记之间的任何格式都会立即丢失,任何会导致刷新这些字段的内容。相反,如果您取消链接 INCLUDETEXT 字段,它们将不再更新以反映基础数据的更改。因此,也许最好的方法是锁定字段,而不是取消链接,这样它们只能按需解锁和刷新。

标签: vba ms-word textfield


【解决方案1】:

为了使用 Word 的对象模型(例如 VBA)将动态字段内容转换为静态文本,需要 Fields.Unlink 方法。对于整个文档:

ActiveDocument.Fields.Unlink

这对于任何给定的Range 也是可能的;删除最后一段中的字段,例如:

ActiveDocument.Paragraphs.Last.Range.Fields.Unlink

为了仅取消链接特定类型的字段,循环 Fields 集合,测试 Field.Type 并相应地取消链接。例如,对于 IncludeText:

Sub DeleteIncludeTextFields()
    Dim doc As word.Document

    Set doc = ActiveDocument
    Debug.Print DeleteFieldType(wdFieldIncludeText, doc)
End Sub

Function DeleteFieldType(fldType As word.WdFieldType, doc As word.Document) _
         As Long

    Dim fld As word.Field
    Dim counter As Long

    counter = 0
    For Each fld In doc.Fields
        If fld.Type = wdFieldIncludeText Then
            fld.Unlink
            counter = counter + 1
        End If
    Next

    DeleteFieldType = counter
End Function

假设您希望在更新后对文档中的所有字段执行此操作:

Sub AutoOpen()
  With Options
    .UpdateFieldsAtPrint = True
    .UpdateLinksAtPrint = True
  End With
  ActiveDocument.Fields.Update
  ActiveDocument.Fields.Unlink
End Sub

【讨论】:

  • 非常感谢,你帮了我很多;)
猜你喜欢
  • 1970-01-01
  • 2012-02-19
  • 2019-12-14
  • 1970-01-01
  • 2019-06-26
  • 2019-09-04
  • 1970-01-01
  • 1970-01-01
  • 2016-05-15
相关资源
最近更新 更多