Log in

View Full Version : Solved: Delete Word table based on text immediately above...



Jinky Julie
09-15-2009, 10:43 AM
Hi all...

I wish to delete any table that occur immediately after a line of text
(paragraph) that is red.

Example:

paragraph 1
table
paragraph 2
table <== DELETE
paragraph 3
table
paragraph 4
table <== DELETE

Note that the red text can appear anywhere (could be any of the four paragraphs) and there is always a table immediately after...

I've tried recording a macro for direction (not pretty)... It seems to work, but it keeps crashing at the last table selection Code 5941 Requested member of the collection does not exist.

Sub killTables()

Selection.Find.ClearFormatting
With Selection.Find
.Font.Color = wdColorRed
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.Tables(1).Select
Selection.Cut
With Selection.Find
.Font.Color = wdColorRed
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.Tables(1).Select
Selection.Cut
With Selection.Find
.Font.Color = wdColorRed
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.Tables(1).Select
Selection.Cut
With Selection.Find
.Font.Color = wdColorRed
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.Tables(1).Select 'crashes here
Selection.Cut
Selection.Find.ClearFormatting
End Sub

Any suggestions... direction.... would be appreciated...

Thanks as always...

J

Tinbendr
09-15-2009, 08:40 PM
Sub DelAfterRed()
Dim Rng As Range


Set Rng = ActiveDocument.Range


Do
With Rng.Find
.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Font.Color = wdColorRed
.Execute
End With

If Rng.Find.Found Then
Rng.MoveStart wdTable
Rng.MoveEnd wdTable
Rng.Cut
End If

Loop Until Not Rng.Find.Found
End Sub


Hope this helps!

Jinky Julie
09-16-2009, 04:35 AM
Tinbendr,

You Rock!!! Works great!!!! Thank you so very much!!!

Some of the things you used, I've never seen before... I learn so much from you guys...

Best regards.

Julie

fumei
09-16-2009, 08:44 AM
Julie, you will improve even more if you really start to do something that has been suggested to you a number of times. Try to stop using Selection. Try to understand and use Range.

macropod
09-16-2009, 11:04 PM
Hi JulieJinky,

Please see: http://www.excelguru.ca/node/7 (mhtml:{2603C627-B617-484B-B3E3-521BCFB808E8}mid://00000106/!x-usc:http://www.excelguru.ca/node/7)

It isn't polite to ask for the same problem to be solved in multiple forums when you don't let on in each that that's what you're doing.

For anyone else, see the discussion in microsoft.public.word.programming