Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Delete Multiple Tables Based on common word

1 view
Skip to first unread message

LycanT

unread,
Jul 18, 2007, 5:39:39 AM7/18/07
to
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

unread,
Jul 18, 2007, 10:18:06 AM7/18/07
to
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

Helmut Weber

unread,
Jul 18, 2007, 10:39:56 AM7/18/07
to
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"

David Sisson

unread,
Jul 18, 2007, 1:30:02 PM7/18/07
to
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

unread,
Jul 18, 2007, 5:12:32 PM7/18/07
to
Hi,

I hope someone is able to help...

I have a document which contains multiple tables.... There are several
tables (# changes on a daily basis) spread throughout the document
which contain the words "GEAR CHANGES". Is there a way to create a
piece of code that finds the word "GEAR CHANGES" and then proceeds to
delete the table?

Cheers
Andrew

Lene Fredborg

unread,
Jul 18, 2007, 6:50:01 PM7/18/07
to
Did you check the answers to your previous post where you asked the same
question?
You will find three different macro solutions.

http://www.microsoft.com/office/community/en-us/default.mspx?&lang=en&cr=US&guid=&sloc=en-us&dg=microsoft.public.word.vba.general&p=1&tid=7d968004-f972-4541-8b64-b58cb0ad13de

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

LycanT

unread,
Jul 20, 2007, 5:31:18 AM7/20/07
to
=?Utf-8?B?RGF2aWQgU2lzc29u?= <David...@discussions.microsoft.com>
wrote in news:0F6F8207-8714-434D...@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

LycanT

unread,
Jul 20, 2007, 5:32:45 AM7/20/07
to
=?Utf-8?B?TGVuZSBGcmVkYm9yZw==?= <l...@REMOVETHISthedoctools.com> wrote in
news:AF4B9D6C-B9DB-444D...@microsoft.com:

> Did you check the answers to your previous post where you asked the
> same question?
> You will find three different macro solutions.

Sorry, was posting via google.groups and never got a confirmation
it had actually worked.

Found an alternate newreader and saw your previous post. Appreciate the
help. Macro is working awesome! :)

0 new messages