identifying tables in document for deletion

B

Brian

Hello all,

I'm stuck again. I was helped in identifying the tables that have 2 columns,
but unfortunately, there are some I don't want to delete. However, those that
are to be deleted have the text "Actio" in the second column of the first
row. I hav tried a few options, but now I get a type mismatch on the line -
Set myrange = ActiveDocument.Tables(1).Cell(2, 1).Range.Text

The full coide is:

Sub delTables()

Dim nCounter As Long
Dim nNumTables As Long
Dim oTable As Word.Table
Dim c As Word.Cell
Dim myrange As Range

'Find out how many tables there are
nNumTables = ActiveDocument.Tables.Count

'Start at the last table, and work backwards
'through the tables in the document.
'We do this because we are deleting tables,
'and the index would go awry as we cycled
'through them.
For nCounter = nNumTables To 1 Step -1

'Get a reference to this table
Set oTable = ActiveDocument.Tables(nCounter)
Set myrange = ActiveDocument.Tables(1).Cell(2, 1).Range.Text


'With oTable
' .Columns.Count = 2
' .Cell(2


'If the table has 2 columns...
If oTable.Columns.Count = 2 And myrange.Text = "Action" Then

'If oTable.Cell(1, 2).Text = "Action" Then
'...delete it
oTable.Delete
End If
End If
Next nCounter

End Sub

Any suggestions on where I am going wrong?

Thanks
 
D

Doug Robbins - Word MVP

By including the .Text at the end of the Set command, you are trying to
assign a string to a range variable. Delete the .Text from that command and
the use:

myrange.End = myrange.End -1 ' to remove the end of cell character from the
range

then

If myrange.Text = "Action" then
myrange.Tables(1).Delete
End If

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
R

Russ

Bryan,
Try This:

Sub delTables()
Dim oTable As Word.Table
For each oTable in ActiveDocument.Tables
If oTable.Columns.Count = 2 And oTable.Cell(2,1).Range.Text = "Action" Then
oTable.Delete
End If
Next oTable
End Sub

I think with counters, you should count backwards, but I don't think it is
necessary with for 'each loops' and 'for each' loops are faster than counter
loops.
 

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