vba - 仅查找样式为 "Heading 1"的文本(Range.Find 以匹配样式)

标签 vba ms-word

我试图在我的文档中找到一些只出现在“标题 1”样式中的文本。到目前为止,无济于事。

示例代码:

With ThisDocument.Range.Find
    .Text = "The Heading"
    .Style = "Heading 1" 'Does not work
    .Execute
    If .Found Then Debug.Print "Found"
End With

请注意,它一直停在目录处。

编辑:修复了拼写错误的“if”语句

最佳答案

你的代码对我来说看起来不错。我最好的猜测是“标题 1”样式存在于您的目录中?

下面的代码应该继续查找,找到所有出现的:

Dim blnFound As Boolean

With ThisDocument.Range.Find
    .Text = "The Heading"
    .Style = "Heading 1"

    Do
        blnFound = .Execute
        If blnFound Then
            Debug.Print "Found"
        Else
            Exit Do
        End If
    Loop
End With

我希望这有帮助。

关于vba - 仅查找样式为 "Heading 1"的文本(Range.Find 以匹配样式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9286927/

相关文章:

excel - 将代码放入循环中,以便在单击任何鼠标后退出循环

excel - 提取Excel单元格中字符串的一部分

vba - 打开工作簿时关闭 Excel 后台错误检查

vba - 在 Word 中反转跟踪更改的宏?

vba - 使用 VBA 在 MS Word 中创建表格

c# - Word VSTO - 向 Word 文件添加标签

python - 使用 python 的 doc、docx 或 rtf 文件中的页数

vba - 如何防止 VBA 编辑器删除内部空格?

excel - VBA 自函数返回#VALUE!单元格出错,而在函数窗口中正确返回实际值

c# - 如何在 C# 字互操作中退格?