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
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
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"
'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
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
--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
> 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
> 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! :)