Delete Multiple Tables Based on common word

L

LycanT

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
 
L

Lene Fredborg

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.

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

Helmut Weber

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.


--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

David Sisson

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
 
L

LycanT

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
 

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