Identify tables with 2 columns and delete

B

Brian

Hi all,

I am trying to find all tables containing only 2 columns in a document,
select them, and then delete. Getting no where fast. Any suggestions
gratefully received.

Sub delTables()
Dim oTbl As Table
For Each oTbl In ActiveDocument.Tables
With ActiveDocument.Tables
If NumColumns = 2 Then
'If ActiveDocument.Tables.Count >= 1 Then

ActiveDocument.Tables.Select
Selection.Delete
End If
End With
Next oTbl

End Sub

Thanks,
 
S

Shauna Kelly

Hi Brian

Try this....

Sub delTables()

Dim nCounter As Long
Dim nNumTables As Long
Dim oTable As Word.Table

'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)

'If the table has 2 columns...
If oTable.Columns.Count = 2 Then
'...delete it
oTable.Delete
End If

Next nCounter

End Sub

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
B

Brian

Thank you Shauna.

It worked a treat. One more question if its not too cheeky, Would it be
possible to not only identify the table to delete by the number of columns,
but also by the word Action in the second column of the table header row? At
the moment, I copy the relevant section to a new document, tidy the section,
then copy it back.

I am stripping out screen captures and step tables from large documents cica
300 pages to reduce to 30 pages. SOPs.

Thanks,
 
R

Russ

This is from VBA Help for the columns collection:
MsgBox ActiveDocument.Tables(1).Columns.Count
Or in your case


Sub delTables()
Dim oTbl As Table
'If ActiveDocument.Tables.Count >= 1 Then
For Each oTbl In ActiveDocument.Tables
If oTbl.Columns.Count = 2 then
oTbl.Delete
' or oTbl.Range.Delete
End If
Next oTbl
'End If
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