Microsoft Word VBA-如果单元格包含指定字符串,请选择表格

Microsoft Word VBA-如果单元格包含指定字符串,请选择表格,vba,ms-word,Vba,Ms Word,创建Microsoft Word宏时遇到问题。这是我正在处理的宏。它成功地选择word文档中的每个表 Sub FindSpecificTables() Selection.WholeStory Dim iResponse As Integer Dim tTable As Table 'If any tables exist, loop through each table in collection. For Each tTable In Active

创建Microsoft Word宏时遇到问题。这是我正在处理的宏。它成功地选择word文档中的每个表

Sub FindSpecificTables()
    Selection.WholeStory

    Dim iResponse As Integer
    Dim tTable As Table

    'If any tables exist, loop through each table in collection.
    For Each tTable In ActiveDocument.Tables
        tTable.Select
        If response = vbNo Then Exit For 'User chose to leave search.
    Next
    MsgBox prompt:="Search Complete.", buttons:=vbInformation
End Sub
但是,如果表包含指定的字符串,我只需要选择一个表。这应该很简单,但我想不出来。如何在表中搜索特定字符串

我已尝试使用以下条件语句调整代码:
如果tTable.Cell(1,1)=“已调整:”则tTable.Select
;见下面的例子


不幸的是,这不起作用。我的语法错了吗?你们有什么建议吗

尝试不同的方法。。。不要循环每个表循环
search(find)
方法并检查找到的文本是否在表中。以下是简单的解决方案:

Sub Find_Text_in_table()

    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "donec"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With

    Do While Selection.Find.Execute

        If Selection.Information(wdWithInTable) Then

            Stop
            'now you are in table with text you searched
            'be careful with changing Selection Object
            'do what you need here
        End If
    Loop
End Sub

确保调用
.Find.Replacement.ClearFormatting
,因为
.Replacement
有一个
.ClearFormatting
.ClearFormatting
分开。从…起
Sub Find_Text_in_table()

    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "donec"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With

    Do While Selection.Find.Execute

        If Selection.Information(wdWithInTable) Then

            Stop
            'now you are in table with text you searched
            'be careful with changing Selection Object
            'do what you need here
        End If
    Loop
End Sub