Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2007

Tip: Looking for answers? Try searching our database.

Identify tables with 2 columns and delete

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Brian - 22 Oct 2007 08:12 GMT
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,
Signature

Brian McCaffery

Shauna Kelly - 22 Oct 2007 08:23 GMT
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

> Hi all,
>
[quoted text clipped - 18 lines]
>
> Thanks,
Brian - 22 Oct 2007 08:41 GMT
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,

Signature

Brian McCaffery

> Hi Brian
>
[quoted text clipped - 56 lines]
> >
> > Thanks,
Russ - 22 Oct 2007 08:49 GMT
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
> Hi all,
>
[quoted text clipped - 18 lines]
>
> Thanks,

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID


Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.