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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top