仅查找样式“标题 1”的文本(范围.查找以匹配风格)

发布于 2025-01-05 09:20:40 字数 312 浏览 2 评论 0原文

我试图在文档中找到一些仅以“标题 1”样式出现的文本。到目前为止,没有任何效果。

示例代码:

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

只是一个注释,它一直停在目录处。

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

I am trying to find some text in my document that only appears in "Heading 1" styles. So far, no avail.

Sample Code:

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

Just a note, it keeps stopping at the table of contents.

Edit: fixed the mispelt 'if' statement

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

于我来说 2025-01-12 09:20:40

你的代码对我来说看起来不错。我最好的猜测是您的目录中存在“标题 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

我希望这会有所帮助。

Your code looks good to me. My best guess is that the 'Heading 1' style exists in your table of contents?

The code below should continue the find, finding all occurrences:

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

I hope this helps.

泪冰清 2025-01-12 09:20:40

我认为它不起作用的原因是您必须设置

.format = true

标志,并且您可能必须通过 .Styles 方法指定样式:

With ThisDocument.Range.Find
    .Text = "The Heading"
    .Format = true   <<< -------- Tells Word to look for a special formatting
    .Style = ThisDocument.Styles("Heading 1")

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

The reason I think it is not working is because you have to set the

.format = true

flag, and you may have to specify the style via the .Styles method:

With ThisDocument.Range.Find
    .Text = "The Heading"
    .Format = true   <<< -------- Tells Word to look for a special formatting
    .Style = ThisDocument.Styles("Heading 1")

    Do
        blnFound = .Execute
        If blnFound Then
           Debug.Print "Found"
        Else
           Exit Do
        End If
    Loop
End With
四叶草在未来唯美盛开 2025-01-12 09:20:40

我在谷歌上发现了这个问题,问题中的代码对我不起作用。我进行了以下更改来修复它:

  • 我将 Selection.Find.Style = "Heading 1" 更改为一个对象。
  • 我更改了代码以从 .Execute 而不是 .Found 获取搜索的布尔结果,

我希望这对其他一些 Google 员工有所帮助。

With ThisDocument.Range.Find
    .Text = "The Heading"
    .Style = ActiveDocument.Styles("Heading 1")

    Dim SearchSuccessful As Boolean
    SearchSuccessful = .Execute

    If SearchSuccessful Then
        ' code
    Else
        ' code
    End If
End With

I found this question on Google and the code in the question did not work for me. I have made the following changes to fix it:

  • I changed Selection.Find.Style = "Heading 1" to an object.
  • I changed the code to grab the boolean result of the search from .Execute rather than .Found

I hope this helps some other Googlers.

With ThisDocument.Range.Find
    .Text = "The Heading"
    .Style = ActiveDocument.Styles("Heading 1")

    Dim SearchSuccessful As Boolean
    SearchSuccessful = .Execute

    If SearchSuccessful Then
        ' code
    Else
        ' code
    End If
End With
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文