在定义的段落中的 table 中查找字符串的宏

Macro to find string in table in defined paragraph

我有结构定义的 MS Word 文档:

5 标题 1

5.1 标题 2

5.1.1 标题 3

。 . .

5.1.7 标题 3

我需要在所有项目“5.X.7”中搜索 table 个结果。

知道如何 select 项目“5.X.7”中第一个 table 的第一行第一列吗?

由于您是新手,即使 Whosebug 不是免费的编码服务...尝试类似的方法来帮助您入门。

Sub FindTables()
    Dim doc As Word.Document, rng As Word.Range, hRng As Word.Range
    Dim splitStr() As String, tbl As Word.Table
    Set doc = ActiveDocument
    Set rng = doc.Content
    With rng.Find
        .ClearFormatting
        .Format = True
        .Forward = True
        .Style = doc.Styles("Heading 3").NameLocal
        .Text = ""
        .Wrap = wdFindStop
        .Execute
        Do While .found = True
            splitStr = Split(rng.ListParagraphs(1).Range.ListFormat.ListString, ".")
            If splitStr(0) = 5 And splitStr(2) = 7 Then
                Set hRng = rng.Bookmarks("\HeadingLevel").Range
                If hRng.Tables.Count > 0 Then
                    Set tbl = hRng.Tables(1).Range
                    'do something with the table
                End If
                rng.Collapse Word.WdCollapseDirection.wdCollapseEnd
            Else
                rng.Collapse Word.WdCollapseDirection.wdCollapseEnd
            End If
        .Execute
        Loop
    End With
End Sub