VBA:如果单词table中的第一个单元格具有特定值,则执行操作,否则转到下一个table

VBA: Do action if first cell in a word table has a certain value, otherwise go to next table

我正在尝试通过来自 Excel 的 VBA 代码来操纵多个 Word table。 Word 文档中有多个 table 需要填充,所以我认为我应该编写一个代码来遍历每个 table 并确定第一个 table 具有字符串 " att" 在第一个单元格 (1,1) 中。如果是这样,那么我会将 Excel 中的单元格复制到特定的 table 中。完成此操作后,它将转到下一个 table 并从电子表格中的另一个位置粘贴。到目前为止,这是我想出的代码,但问题是它无法识别 table 单元格中的文本,所以一直到 i=30。它只是跳过第一个单元格中具有“att”的 table。有什么建议吗?

Sub UpdateTables()
    Dim documentname As String
    Dim aktDocument As Document
    Dim tbl As Word.Table
    Dim i As Integer
    Dim g As Integer
    Dim xWB As Workbook

    documentname = InputBox("Paste or Type the name of the cycle report word document")
    Documents(documentname).Select

    i = 1
   
    Do Until i > 30      ' Loop through each table in the document and stop when the first cell says attribute in it 
        Set aktDocument = ActiveDocument
        Set tbl = aktDocument.Tables(i)
    
        tbl.Select
    
        If tbl.Cell(1, 1).Range.Text = "Att" Then ' Not recognizing text inside cell with this
            'Populate this graph and then populate the next few graphs with summary table info
            'Exit Loop below by making i = 31
            i = 31
        Else 'If first cell is not Att then end the if statement and loop again
        End If
       
        i = i + 1
    Loop
   
End Sub

不要测试等价性 (=),而是测试 Table 的 Cell(1,1) 中“Att”的存在 (Instr)。

Sub UpdateTables()
    
    Do
    
        Dim documentname As String
        documentname = InputBox("Paste or Type the name of the cycle report word document")
        
    Loop Until Len(documentname) > 0
    
    Dim myDoc As Word.Document
    Set myDoc = Documents(documentname)
    
    Dim myTable As Word.Table
    For Each myTable In myDoc.StoryRanges(wdMainTextStory).Tables   ' Loop through each table in the document and stop when the first cell says attribute in it
    
        If InStr(myTable.Cell(1, 1).Range.Text, "Att") > 0 Then ' Not recognizing text inside cell with this
            
            Dim myWb As Excel.Workbook
            'Populate this graph and then populate the next few graphs with summary table info
            
            ' If you need to process additional tables before exiting the loop 
            ' not as part of the loop then 
            ' consider using the next method to get the next table in the document.
            ' Set myTable = myTable.Range.Next(unit:=wdTable)
            Exit For
            
        Else 'If first cell is not Att then end the if statement and loop again
        
        End If
        
    Next
   
End Sub