Thread: Table Deletions
View Single Post
 
Old 09-01-2021, 11:38 AM
ranjan ranjan is offline Windows 10 Office 2019
Advanced Beginner
 
Join Date: May 2021
Posts: 77
ranjan is on a distinguished road
Default

Hi,

Can anyone review the below code... Its working partially only...Its not working for the below tables deletion.

Code:
Sub TABLE_TEST()
Application.ScreenUpdating = False
Dim Rng As Range
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "AAA"
    .Replacement.Text = ""
    .Format = False
    .Forward = False
    .Wrap = wdFindStop
    .MatchCase = True
    .MatchWholeWord = False
    .MatchWildcards = False
  End With
  Do While .Find.Execute
    If .Information(wdWithInTable) = True Then
      Set Rng = ActiveDocument.Range(0, .Tables(1).Range.Start)
      Do While Rng.Tables.Count > 0
        Rng.Tables(1).Delete
      Loop
      Exit Do
    End If
    .Collapse wdCollapseStart
  Loop
End With
   
   With ActiveDocument.Range
    With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "BBB:"
    .Replacement.Text = ""
    .Format = False
    .Forward = False
    .Wrap = wdFindStop
    .MatchCase = True
    .MatchWholeWord = False
    .MatchWildcards = False
  End With
  
    Do While .Find.Execute
    If .Information(wdWithInTable) = True Then
      Set Rng = ActiveDocument.Range(0, .Tables(1).Range.Start)
            Do While Rng.Tables.Count > 0
        Rng.Tables(1).Delete
      Loop
      Exit Do
    End If
    .Collapse wdCollapseStart
     Loop
End With
Application.ScreenUpdating = True
End Sub
Reply With Quote