Loop / delete tables

M

Mat

I have a bit of code that finds tables in a document and
deletes them - it is a simple do...loop. When I run it it
loops through OK, but I get an error on the last but one
line (see below)
How can I run this macro without the error (eg how to
exit the loop/macro without an error ?
Thanks for any help ...
Mat


Do
Selection.GoTo What:=wdGoToTable, Which:=wdGoToNext,
Count:=1, Name:=""
Selection.Find.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Tables(1).Select <<<<<< Code Error here
Selection.Tables(1).Delete
Loop

End Sub
 
P

Pat Garard

G'Day Mat,

Try something like:

Sub DeleteAllTables()
Dim myTable As Table
With ActiveDocument
For Each myTable In .Tables
myTable.Delete
Next
End With
End Sub
--
Regards,
Pat Garard
Australia

______________________________________
 
J

Jay Freedman

Hi Mat

The best fix is not to do it that way at all. :) The Word object
model gives you a Tables collection for the whole document, so you can
zap tables without ever having to select them.

Sub ZapAllTables()
Dim oTbl As Table
For Each oTbl In ActiveDocument.Tables
oTbl.Delete
Next oTbl
End Sub
 
Top