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 / July 2007

Tip: Looking for answers? Try searching our database.

Delete Multiple Tables Based on common word

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
LycanT - 18 Jul 2007 10:39 GMT
Hi,

Help, I'm stuck :)

I want to delete the tables from a word document that contain the
words "GEAR CHANGES".
Unfortunately this document is updated on a daily basis and there
could be 10 tables tomorrow and 3 the next.

I've managed to figure out how to get the first one deleted but I'm
not clever enough to figure out what commands are required to get it
to carry on till all tables are deleted.

This is what I have:

Sub DeleteTable()
'
'Delete Tables with GEAR CHANGES inside it.
'
   With Selection.Find
       .Text = "GEAR GHANGES"
       .Replacement.Text = ""
       .Forward = True
       .Wrap = wdFindContinue
       .Format = False
       .MatchCase = True
       .MatchWholeWord = False
       .MatchWildcards = False
       .MatchSoundsLike = False
       .MatchAllWordForms = False
   End With
   Selection.Find.Execute
   Selection.Tables(1).Select
   Selection.Tables(1).Delete

Can I make it loop till it cant find anymore then carry on with the
rest of the macro?

Cheers
Andrew
Lene Fredborg - 18 Jul 2007 15:18 GMT
The macro below should do what you want. The macro iterates through all
tables in the active document. If "GEAR CHANGES" (uppercase as here) is found
in the table, the entire table is deleted.

Sub DeleteAllTablesWithSpecificString()

   Dim oTable As Table

   For Each oTable In ActiveDocument.Tables
       If InStr(1, oTable.Range.Text, "GEAR CHANGES", vbTextCompare) > 0 Then
           oTable.Delete
       End If
   Next oTable

End Sub

In this case, I found it easier to use another approach than Find - but your
macro could have been adjusted instead.

Signature

Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word

> Hi,
>
[quoted text clipped - 36 lines]
> Cheers
> Andrew
Helmut Weber - 18 Jul 2007 15:39 GMT
Hi Andrew,

like this:

Sub Test3()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
  .Text = "GEAR CHANGE"
  .MatchCase = True
  While .Execute
     If rDcm.Information(wdWithInTable) Then
        rDcm.Tables(1).Delete
     End If
  Wend
End With
End Sub

The strange thing is, that if you search in rDcm
and find something, rDcm shrinks to the found spot.
So rDcm.tables(1) is not the table 1 of the doc,
but the actual table in which "GEAR CHANGE" was found.

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"

David Sisson - 18 Jul 2007 18:30 GMT
Sub DeleteTables()

'I've always read, that when deleting, it's best to start at the bottom.
'Here's another example.

Dim oDoc As Document
Dim NumTbl As Integer
Dim A As Integer

Set oDoc = ActiveDocument
'Total number of tables in document
NumTbl = oDoc.Tables.Count

'Count backwards through the tables, deleting as necessary.
For A = NumTbl To 1 Step -1
   'Borrowing from Lene
   If InStr(1, oDoc.Tables(A).Range.Text, "GEAR CHANGES", vbTextCompare) >
0 Then
       oDoc.Tables(A).Delete
   End If
Next A
End Sub
LycanT - 20 Jul 2007 04:31 GMT
=?Utf-8?B?RGF2aWQgU2lzc29u?= <DavidSisson@discussions.microsoft.com>
wrote in news:0F6F8207-8714-434D-8CC2-8BF4F613BE06@microsoft.com:

> Sub DeleteTables()
>
> 'I've always read, that when deleting, it's best to start at the
> bottom. 'Here's another example.
> Next A
> End Sub

Many thanks to all that helped. My macro is running prefectly. Help
very much appreciated.

Cheers
Andrew
 
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.