【问题标题】:Microsoft Word VBA Macro - One Paragraph Find-Replace StylesMicrosoft Word VBA 宏 - 一个段落查找替换样式
【发布时间】:2016-07-20 19:58:21
【问题描述】:

我正在使用 VBA 宏在 Microsoft Word 中执行样式搜索。 我的目标是对文档中找到的每种样式执行一次特定操作。

宏在至少有两个段落的文档上可以正常工作,但宏不会在只有一个段落的文档中正确警告样式。奇怪的是,当我输入一个新的段落标记时,找到了样式,即使我没有在文档中添加任何新的文本或样式,只是一个额外的空白段落标记。有谁知道我的宏有什么问题以及如何解决这个问题?感谢您的观看。

Sub AlertAllStylesInDoc()
    Dim Ind As Integer
    Dim numberOfDocumentStyles As Integer
    Dim styl As String
    Dim StyleFound As Boolean

    numberOfDocumentStyles = ActiveDocument.styles.count

    For Ind = 1 To numberOfDocumentStyles
        styl = ActiveDocument.styles(Ind).NameLocal
        With ActiveDocument.Content.Find
            .ClearFormatting
            .text = ""
            .Forward = True
            .Format = True
            .Style = styl
            Do
                StyleFound = .Execute
                If StyleFound = True Then
                    ' actual code does more than alert, but keeping it simple here'
                    MsgBox styl
                    GoTo NextStyle
                Else
                    Exit Do
                End If
            Loop
        End With

    NextStyle:
        Next

End Sub   

【问题讨论】:

    标签: vba ms-word find-replace msstyles


    【解决方案1】:

    我不明白为什么 ActiveDocument.Content 不起作用,但用 ActiveDocument.Range(0,0) 替换它似乎可以解决问题(在 Word 2016 中测试)。

    With ActiveDocument.Range(0, 0).Find

    【讨论】:

    • 谢谢!这是一个很大的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    • 2013-10-20
    • 2021-04-28
    • 1970-01-01
    相关资源
    最近更新 更多