【问题标题】:Delete all tables that don't contain certain strings VBA Word删除所有不包含某些字符串的表 VBA Word
【发布时间】:2020-03-19 01:48:07
【问题描述】:

我正在尝试删除 Word 文档中不包含某些字符串的所有表格,但没有成功。

我当前的代码是:

Sub DeleteTablesIf()
    Dim t As Table
    For Each t In ActiveDocument.Tables
        If t not contains  "A" or  "B"  "C" then
         t.Delete
        End if 
    Next
End Sub

但我不知道如何检查每个表中是否存在字符串“A”或“B”或“C”。

提前感谢您的帮助。

【问题讨论】:

    标签: vba ms-word


    【解决方案1】:

    未经测试:

    Sub DeleteTablesIf()
        Dim t As Table, i As Long, txt As String
    
        'loop backwards when deleting
        For i = ActiveDocument.Tables.Count To 1 Step -1
            With ActiveDocument.Tables(i)
                txt = .Range.Text
                If Instr(txt, "A") = 0 And  Instr(txt, "B") = 0 And _
                                            Instr(txt, "C") = 0 Then 
                    .Delete
                End if 
            End With
        Next
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2021-04-28
      • 1970-01-01
      • 2021-04-07
      • 1970-01-01
      • 2015-06-16
      • 2019-09-14
      • 1970-01-01
      • 2012-02-18
      • 2020-09-11
      相关资源
      最近更新 更多