PDA

View Full Version : Deleting all lines that contain a certain color



bobsacaman01
05-09-2011, 09:13 AM
Hello,

I currently have a macro in place that will color certain words within a text. I'm wondering if there is a way to delete a whole line that contains a certain color.

For example, the following is a bit of my text in Word. I've highlighted every instance of "Kirkendoll"...is there a way within Word to delete all the lines containing "Kirkendoll" whether by doing a "Find/Replace" or writing a macro? I'm not sure...

Thanks in advance!!


Ut 1 10 Ut24 Gilbert Garret P C Whittaker Fosw 5 UT29 Lewis Travis
Ut 2 5 Ut29 Gilbert Garret P C Kirkendoll Jam 6 UT35 Wort Tom PEN UT
Ut 2 15 Ut19 Gilbert Garret P C Goodwin Marqui 15 UT34 Nelson Jonatha
Ut 2 10 Ut24 Gilbert Garret P C Matthews Barre 5 UT29 Wort Tom
Ut 3 5 Ut29 Gilbert Garret P C Kirkendoll Jam 9 UT38 UT Hurst
Ut 1 10 Ut38 Gilbert Garret P C Whittaker Fosw 2 UT40 Lewis Travis
Ut 2 8 Ut40 Gilbert Garret P C Kirkendoll Jam 5 UT45 Wort Tom
Ut 3 3 Ut45 Gilbert Garret P I Kirkendoll Jam dropped P
Ut 4 3 Ut45 Roberson Ryan R . . . 3 UT48 UT Wort Tom

Frosty
05-09-2011, 04:17 PM
There are a lot of ways to do what you want to do. And lots of examples of using the .Find object on the forum. To help you learn how to do it, it would probably help to post your existing code, rather than giving you an entirely different methodology.

As always, a recorded macro will help get you on your way (don't worry about doing a replace all, just find one instance, then use the keyboard to select the whole paragraph in whatever combination of home key, shift + end key, etc). And then adding a loop is easy as pie.

It will end up looking something like this (code snippet, won't work as is)

dim rngSearch as Range
dim oPara as Paragraph
set rngSearch = ActiveDocument.Content
With rngSearch.Find
.Text = "Kirkendoll"
'other find criteria yada yada
Do until .Execute = False
set oPara = rngSearch.Paragraphs(1)
oPara.Range.Delete
Loop
End With

Frosty
05-09-2011, 04:18 PM
Of course, the above methology fails if your definition of a "line" is not a paragraph, but rather a soft-return, or a table row.

There is also a way of doing the above using a wildcard search, although that's pretty tricky, the knowledge gained by playing with wildcard searching can pay a lot of dividends down the road.

Hope that helps.