how to highlight found word?

G

Geoff Cox

Hello

following code will find a red fonted word but how would I change it
to highlight the word once found?

Cheers

Geoff

Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Font.Color = wdColorGreen



End With
 
S

Shauna Kelly

Hi

Try something like this:

Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Font.Color = wdColorRed
.Execute
If .Found Then
oRng.Select
End If
End With

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
G

Greg Maxey

Wouldn't your code find "Green" ;-)


Sub Test()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Font.Color = wdColorRed
While .Execute
oRng.HighlightColorIndex = wdDarkYellow
oRng.Collapse wdCollapseEnd
Wend
End With
End Sub
 
G

Geoff Cox

Hi

Try something like this:

Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Font.Color = wdColorRed
.Execute
If .Found Then
oRng.Select
End If
End With

Hope this helps.

Thanks Shauna.

Cheers

Geoff
 
G

Geoff Cox

Hi

Try something like this:

Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Font.Color = wdColorRed
.Execute
If .Found Then
oRng.Select
End If
End With

Shauna,

the above finds and highlights the first red word found - but can I
highlight all the red fonted words?

Cheers

Geoff
 
H

Helmut Weber

Hi Geoff,

Sub Macro2a()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Font.Color = wdColorRed
While .Execute
oRng.HighlightColorIndex = wdYellow
Wend
End With
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Geoff Cox

Hi Geoff,

Sub Macro2a()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Font.Color = wdColorRed
While .Execute
oRng.HighlightColorIndex = wdYellow
Wend
End With
End Sub

Many thanks Bavaria!

I wonder how I could go a step further and find/highlight those red
fonted words on a line which has a blank line before and after it?

Cheers

Geoff

PS any suggestions as to where I might find answers to really basic
VBA questions - without troubling kind souls such as yourself?!
 
S

Shauna Kelly

Hi Geoff
find/highlight those red
fonted words on a line which has a blank line before and after it?

I could guess at what you mean, but it would be better for all of us if you
could tell us more precisely what you're looking for. Word has only the
haziest idea of what a "line" is, because Word constantly interrogates the
printer driver and wraps text within a paragraph according to the rules you
have specified (paper size, margins, font etc) and the printer's capacities.
Word's basic unit of work is the paragraph, not the line.

If you click the ¶ button you'll see a ¶ sign for every paragraph. Some (I
suspect) will have no content. Are you looking for those empty paragraphs?
That is, do you need to find or highlight text in red that is in a paragraph
that is either preceded by an empty paragraph or is followed by an empty
paragraph?

To answer your second question, the first place to look for basic stuff is
to use the macro recorder and analyse (and clean) what it produces. For help
with that, see
Getting To Grips With VBA Basics In 15 Minutes
http://www.word.mvps.org/FAQs/MacrosVBA/VBABasicsIn15Mins.htm

Creating a macro with no programming experience using the recorder
http://www.word.mvps.org/FAQs/MacrosVBA/UsingRecorder.htm

How to modify a recorded macro
http://www.word.mvps.org/FAQs/MacrosVBA/ModifyRecordedMacro.htm


Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
G

Geoff Cox

If you click the ¶ button you'll see a ¶ sign for every paragraph. Some (I
suspect) will have no content. Are you looking for those empty paragraphs?
That is, do you need to find or highlight text in red that is in a paragraph
that is either preceded by an empty paragraph or is followed by an empty
paragraph?

Shauna,

I wish to distinguish between red fonted words which are not in
paragraphs and those that are.

I am looking only for words which form headings. Mostly they have an
empty para before and after but sometimes they appear at the top of a
page - do they then only have an empty para after them? Or, does the
empty para at the bottom of the preceding page count?

I can then use a macro to change their style to that required by a
program called Course Genie which changes Word docs into html pages.

... and many thanks for the suggestions below.

Cheers

Geoff
 
H

Helmut Weber

Hi Geoff,
I wish to distinguish between red fonted words which are not in
paragraphs and those that are.

hmm..., difficult,
as all words are in paragraphs.

Do you want to distinguish between red words
which occupy an entire paragraph
and words which are embedded in not red text?

You may check further whether a paragraph
which contains nothing but a red word is
preceded by an empty paragraph and
followed by an empty paragraph.

Which is not as simple as the piece of
code I've posted earlier.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Geoff Cox

Hi Geoff,


hmm..., difficult,
as all words are in paragraphs.

Do you want to distinguish between red words
which occupy an entire paragraph
and words which are embedded in not red text?
You may check further whether a paragraph
which contains nothing but a red word is
preceded by an empty paragraph and
followed by an empty paragraph.
Which is not as simple as the piece of
code I've posted earlier.

Helmut,

The para above would describe what I want to find - any pointers as to
how to do this?!

Thanks

Geoff
 
H

Helmut Weber

Hi Geoff,

this one highlights red pieces of text
if the preceding paragraph is empty:

Sub Macro2a()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Font.Color = wdColorRed
While .Execute
If Len(oRng.Paragraphs(1).Previous.Range) = 1 Then
oRng.HighlightColorIndex = wdYellow
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"
 
S

Shauna Kelly

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.

Note that this will:
- not find the very first paragraph in the document (because it has no
previous paragraph)
- not find the very last paragraph in the document (no next paragraph)
- not find two consecutive paragraphs containing red text (no empy
paragraphs)
- not find workds outside the main body of the document; it does not work in
headers, footers, comments, footnotes, endnotes etc.

If this is for real production code, you'll need to add suitable error
checking.

If the lines break up in transmission, you might have to glue them back
together.

Public Sub MakeHeadingsFromRedText()

Dim oRng As Word.Range
Dim paraPrevious As Word.Paragraph
Dim paraNext As Word.Paragraph

Set oRng = ActiveDocument.Range
With oRng.Find
.Font.Color = wdColorRed

Do While .Execute

'Get the next and previous paragraphs
On Error Resume Next
Set paraPrevious = oRng.Paragraphs.First.Previous
Set paraNext = oRng.Paragraphs.Last.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.
oRng.Paragraphs(1).Style = wdStyleHeading1

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

End If

End If

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

Loop
End With
End Sub



Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
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.

Many thanks Shauna - will give it a go.

Cheers

Geoff
 
G

Geoff Cox

Hi Geoff,

this one highlights red pieces of text
if the preceding paragraph is empty:

Sub Macro2a()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Font.Color = wdColorRed
While .Execute
If Len(oRng.Paragraphs(1).Previous.Range) = 1 Then
oRng.HighlightColorIndex = wdYellow
End If
Wend
End With
End Sub

thanks Helmut - trying it out now.

Cheers

Geoff
 
G

Geoff Cox

Hi Geoff,

this one highlights red pieces of text
if the preceding paragraph is empty:

Sub Macro2a()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Font.Color = wdColorRed
While .Execute
If Len(oRng.Paragraphs(1).Previous.Range) = 1 Then
oRng.HighlightColorIndex = wdYellow
End If
Wend
End With
End Sub

Helmut,

I get "object variable or with block variable not set" - cannot see
why?

Cheers

Geoff
 
R

Russ

Helmut's sub will give an error if the first paragraph in document has red
font in it, because there is no previous paragraph to the first paragraph.
You'd have to add error checking to avoid that type of error.
 
H

Helmut Weber

Hi everybody,
Helmut's sub will give an error if the first paragraph in document has red
font in it, because there is no previous paragraph to the first paragraph.
You'd have to add error checking to avoid that type of error.

or prevent an error, like this:

Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Font.Color = wdColorRed
While .Execute
' error prevention
' in case there is no preceding paragraph
If Not oRng.Paragraphs(1).Previous Is Nothing Then
If Len(oRng.Paragraphs(1).Previous.Range) = 1 Then
oRng.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"
 
G

Geoff Cox

Hi everybody,


or prevent an error, like this:

Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Font.Color = wdColorRed
While .Execute
' error prevention
' in case there is no preceding paragraph
If Not oRng.Paragraphs(1).Previous Is Nothing Then
If Len(oRng.Paragraphs(1).Previous.Range) = 1 Then
oRng.HighlightColorIndex = wdYellow
End If
End If
Wend
End With
End Sub


fine - thanks Helmut.

Cheers

Geoff
 

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