PDA

View Full Version : First word in paragraph



caveman
07-11-2013, 05:48 AM
Hi experts.
I am stuck with a macro that seems to be easy. But i am stuck anyway

What i need:
i have a word document.
I need to check every paragraph in it, and if there first word of paragraph is BAD - then make a background color red, and if the first word is GOOD - then make a background color green.

i.ve started with classic

Dim par As Word.Paragraph

For Each par In ActiveDocument.Paragraphs


Next


and then suddently stuck (

gmaxey
07-11-2013, 11:00 AM
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oPar As Paragraph
For Each oPar In ActiveDocument.Range.Paragraphs
Select Case oPar.Range.Words(1)
Case Is = "Good": oPar.Range.HighlightColorIndex = wdBrightGreen
Case Is = "Bad": oPar.Range.HighlightColorIndex = wdDarkRed
End Select
Next
End Sub

caveman
07-11-2013, 12:36 PM
Hi Greg, tnanks for such fast reply, unfortunatelly this macro is not working.
Probably i am not made quite clear description of my problem:

i have a text in my document:


BAD Nicholas Angel (Simon Pegg), an extremely dedicated police officer of the London Metropolitan Police Service, performs his duties so well that he is accused of making his colleagues look bad.

GOOD As a result, his superiors transfer him to "crime-free" Sandford, a town in rural Gloucestershire. Once in Sandford, he immediately arrests a large group of under-age drinkers, and a drunk driver who turns out to be his partner, PC Danny Butterman (Nick Frost), the son of local police inspector Frank Butterman (Jim Broadbent).

1st paragraph i need to make a red background and 2nd should become green.

I've run your macro and nothing happens.

Any advice? and tnahks ahead.

gmaxey
07-11-2013, 01:20 PM
Try this:

Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oPar As Paragraph
For Each oPar In ActiveDocument.Range.Paragraphs
Select Case oPar.Range.Words(1)
Case Is = "GOOD ": oPar.Range.Shading.BackgroundPatternColor = wdColorBrightGreen
Case Is = "BAD ": oPar.Range.Shading.BackgroundPatternColor = wdColorDarkRed
Case Else: oPar.Range.Shading.BackgroundPatternColor = wdColorAutomatic
End Select
Next
End Sub

caveman
07-12-2013, 12:54 AM
Greg - working just perfect. minor problems - but i can fix them bymiself/ Thanks alot.