View Single Post
 
Old 02-06-2022, 01:56 AM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,142
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

If you want to delete all the tables up to the one containing the find string, you need a slightly different approach. You will however have to search for the unicode string with English interface which is entered at the top of the code.
If you want to delete the tables up to the table with the first find string (including the table with that string) then:
Code:
Option Explicit

Sub BatchDelete()
Dim sFind As String: sFind = ChrW(32467) & ChrW(31639) & ChrW(22791) & ChrW(20184) & ChrW(37329)
Dim strFile As String
Dim strPath As String
Dim oDoc As Document
Dim iFld As Integer
Dim fDialog As FileDialog
    Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
    With fDialog
        .Title = "Select folder and click OK"
        .AllowMultiSelect = False
        .InitialView = msoFileDialogViewList
        If .Show <> -1 Then
            MsgBox "Cancelled By User", , "List Folder Contents"
            Exit Sub
        End If
        strPath = fDialog.SelectedItems.Item(1)
        If Right(strPath, 1) <> "\" Then strPath = strPath + "\"
    End With
    strFile = Dir$(strPath & "*.*")
    While strFile <> ""
        If Right(LCase(strFile), 4) = ".rtf" Or Right(LCase(strFile), 4) = "docx" Then
            Set oDoc = Documents.Open(strPath & strFile)
            DelTables oDoc, sFind
            oDoc.Close SaveChanges:=wdSaveChanges
        End If
        strFile = Dir$()
    Wend
lbl_Exit:
    Exit Sub
End Sub

Private Sub DelTables(oDoc As Document, sFind As String)
Dim lCount As Long, lDel As Long, lTable As Long
    If oDoc.Tables.Count > 0 Then
        For lCount = 1 To oDoc.Tables.Count
            If InStr(1, oDoc.Tables(lCount).Range, sFind) > 0 Then
                Exit For
            End If
        Next lCount
        If lCount > 0 Then
            For lDel = lCount To 1 Step -1
                oDoc.Tables(lDel).Delete
            Next lDel
        End If
    End If
lbl_Exit:
    Exit Sub
End Sub
If you want to retain the table with the first occurrence and delete the preceding tables then change
Code:
If lCount > 0 Then
            For lDel = lCount To 1 Step -1
to
Code:
If lCount > 1 Then
            For lDel = lCount - 1 To 1 Step -1
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote