Question

I have a VBA macro in Microsoft Word 2007 that finds all tables in my document with a particular background shade color and then deletes that table. That part works fine.

But, in addition to needing to delete the table, I also need to delete the paragraph that follows it. The paragraph that ALWAYS follows is of style "Macro Text" with no text in it. It is there simply to "break up the tables" from each other so that they don't combine into one large table.

How would I do this? Following is my code for deleting the tables:

For Each aTable In ActiveDocument.Tables
    If aTable.Rows(1).Cells(2).Shading.BackgroundPatternColor = wdColorGray15 Then
        aTable.Delete
    End If
Next aTable

No correct solution

OTHER TIPS

At its simplest I think you need something like this. You may need to extend the range to include the entire paragraph, check the style name etc.

Dim aTable As Word.Table
Dim rng As Word.Range
For Each aTable In ActiveDocument.Tables
If aTable.Rows(1).Cells(2).Shading.BackgroundPatternColor = wdColorGray15 Then
  Set rng = aTable.Range
  rng.Move unit:=wdParagraph, Count:=1
  aTable.Delete
  rng.Delete
  Set rng = Nothing
End If
Next aTable

THANKS bibadia! You saved me! Correct answer (for finding grey text in either column of two column tables in ALL tables and then deleting those tables):

Dim aTable As Word.Table
Dim rng As Word.Range
For Each aTable In ActiveDocument.Tables
If aTable.Shading.BackgroundPatternColor = wdColorGray15 Then
    Set rng = aTable.Range
    rng.Move unit:=wdParagraph, Count:=1
    aTable.Delete
    rng.Delete
    Set rng = Nothing
Else
    If aTable.Rows(1).Cells(2).Shading.BackgroundPatternColor = wdColorGray15 Then
        Set rng = aTable.Range
        rng.Move unit:=wdParagraph, Count:=1
        aTable.Delete
        rng.Delete
        Set rng = Nothing
    End If
End If
Next aTable
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top