how to highlight found word?

G

Geoff Cox

On Thu, 30 Aug 2007 09:20:28 +1000, "Shauna Kelly"


Shauna,

Your code works OK on the first red fonted words with empty para
before and after but doesn't work with the other similar red fonted
words.

How do I get ito to go through all the similar red fonted words?

Cheers

Geoff
 
G

Geoff Cox

Hi Geoff


The following will find all paragraphs containing red text that are preceded
*and* followed by an empty paragraph, apply style Heading 1 and delete the
offending empty paragraphs.

Shauna,

I guess I didn't make clear that I want to find paras in which each
and every word has red fonts. I want to ignore cases where there is
say 1 red fonted word in a para of black fonted words.

How is this to be done?

Cheers

Geoff
 
D

Doug Robbins - Word MVP

The following modification of Shauna's code does what you want. Next time,
please state the complete requirement in the initial post.

Dim oPara As Word.Paragraph
Dim paraPrevious As Word.Paragraph
Dim paraNext As Word.Paragraph

For Each oPara In ActiveDocument.Paragraphs
If oPara.Range.Font.Color = wdColorRed Then

'Get the next and previous paragraphs
On Error Resume Next
Set paraPrevious = oPara.Previous
Set paraNext = oPara.Next
On Error GoTo 0

'If *both* the next and previous paragraphs exists
'and are empty then apply style Heading 1 to the
'paragraph containing the red text
If Not paraPrevious Is Nothing And Not paraNext Is Nothing Then

If Len(paraPrevious.Range) = 1 And Len(paraNext.Range) = 1 Then
'The paragraphs before and after the para
'containing the red text are empty.
'Make this a heading.
oPara.Style = wdStyleHeading1

'And delete the extraneous paragraphs
paraPrevious.Range.Delete
paraNext.Range.Delete

End If

ElseIf paraPrevious Is Nothing And Not paraNext Is Nothing Then

If Len(paraNext.Range) = 1 Then
'The paragraph is the first paragraph in the document
'and the paragraph following thatcontaining the red text is
empty.
'Make this a heading.
oPara.Style = wdStyleHeading1

'And delete the extraneous paragraph
paraNext.Range.Delete

End If

End If

'Clean up for next time
Set paraPrevious = Nothing
Set paraNext = Nothing

End If
Next oPara

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
G

Geoff Cox

The following modification of Shauna's code does what you want. Next time,
please state the complete requirement in the initial post.

Many thanks Doug and yes, will be more careful in future re spelling
out more clearly etc.

Cheers

Geoff
 
H

Helmut Weber

Hi Geoff,

getting more and more complicated.

Sub Macro2bbb()
Dim oRngD As Range ' document's range
Dim oPrgP As Paragraph ' previous paragraph
Dim oPrgT As Paragraph ' this paragraph
Dim oPrgN As Paragraph ' next paragraph
Set oRngD = ActiveDocument.Range
With oRngD.Find
.Font.Color = wdColorRed
While .Execute
Set oPrgT = oRngD.Paragraphs(1)
Set oPrgP = oPrgT.Previous
Set oPrgN = oPrgT.Next
If Not (oPrgP Is Nothing) And Not (oPrgN Is Nothing) Then
If Len(oPrgP.Range) = 1 And Len(oPrgN.Range) = 1 Then
oPrgT.Range.HighlightColorIndex = wdYellow
End If
End If
Wend
End With
End Sub


--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top