Range.Find 在搜索 Table 旁边的标题样式时失败

Range.Find fails when searching for a Heading Style next to a Table

我需要在大型文档中执行查找,以将“标题 1”header 的大小写从大写更改为标题大写。我写了下面的代码来演示目的(包括一些丑陋的步骤来帮助调试):

Sub FixHeadings()
    Dim doc As Word.Document
    Dim rng As Word.Range
    
    Set doc = ActiveDocument
    Set rng = doc.Content
        
    With rng.Find
        .Format = True
        .Style = doc.Styles("Heading 1").NameLocal
        .Forward = True
        
        Do While .Execute
            rng.Case = wdTitleWord
            rng.Style = doc.Styles("Heading 1").NameLocal
            resp = MsgBox(rng.Text & vbNewLine & vbNewLine & "Text was found, continue?", vbQuestion & vbYesNoCancel)
            If resp = vbNo Then
                Exit Do
            ElseIf resp = vbCancel Then
                foo = True
            End If
        Loop
    End With
End Sub

我遇到的问题:如果table紧跟在标题之后,Find会卡在当前标题上,无法继续前进。在我添加调试行之前,这会挂起 Word。

代码在 table 不跟在 header 之后的文本上运行良好。但是,我无法控制文档的格式,也不能假设会包含一个空白段落。我确实发现这确实有帮助,但同样,我不能预先假设。

我会使用 Find All 和 Replacement 属性,但它不支持大小写。此外,我尝试使用 Goto 将指针微移到标题之外,但无济于事。

可以做什么?这是一个错误吗?我应该检测下一个是否是 Table 并以某种方式采取行动吗?如果是,应该采取什么行动?

简单:将范围折叠到末尾,按照:

Sub FixHeadings()
Dim Resp
With ActiveDocument.Range
  With .Find
    .Format = True
    .Forward = True
    .Style = wdStyleHeading1
  End With
  Do While .Find.Execute = True
    .Case = wdTitleWord
    Resp = MsgBox(.Text & vbNewLine & vbNewLine & "Text was found, continue?", vbQuestion & vbYesNoCancel)
    If Resp = vbNo Then
      Exit Do
    ElseIf Resp = vbCancel Then
        'foo = True
    End If
    If .End = ActiveDocument.Range.End Then Exit Do
    .Collapse wdCollapseEnd
  Loop
End With
End Sub