Sunday, January 30, 2011

how to highlight found word?



how to highlight found word?

Geoff Cox posted on Sunday, August 26, 2007 10:01 AM

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

 how to highlight found word?

Shauna Kelly posted on Sunday, August 26, 2007 10:34 AM

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

Wouldn't your code find "Green" ;-)Sub Test()Dim oRng As Word.

Greg Maxey posted on Sunday, August 26, 2007 10:40 AM

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

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Hi Geoff,Sub Macro2a()Dim oRng As Word.RangeSet oRng = ActiveDocument.

Helmut Weber posted on Monday, August 27, 2007 11:32 AM

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

how to highlight found word?

Geoff Cox posted on Monday, August 27, 2007 1:20 PM

On Mon, 27 Aug 2007 17:32:10 +0200, Helmut Weber


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?!

Hi GeoffI could guess at what you mean, but it would be better for all of us

Shauna Kelly posted on Monday, August 27, 2007 5:57 PM

Hi Geoff


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/wor

how to highlight found word?

Geoff Cox posted on Tuesday, August 28, 2007 2:08 AM

On Tue, 28 Aug 2007 07:57:12 +1000, "Shauna Kelly"



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

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

Helmut Weber posted on Wednesday, August 29, 2007 11:12 AM

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.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003

Hi Geoff,this one highlights red pieces of text if the preceding paragraph is

Helmut Weber posted on Wednesday, August 29, 2007 7:13 PM

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

Hi GeoffThe following will find all paragraphs containing red text that are

Shauna Kelly posted on Wednesday, August 29, 2007 7:20 PM

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

Helmut's sub will give an error if the first paragraph in document has redfont

Russ posted on Thursday, August 30, 2007 4:58 AM

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.


--
Russ

The following modification of Shauna's code does what you want.

Doug Robbins - Word MVP posted on Saturday, September 01, 2007 5:52 PM

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

Hi Geoff,getting more and more complicated.

Helmut Weber posted on Sunday, September 02, 2007 5:57 AM

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


































Pete's Resume  |  Robbe's Resume  |  Neado  |  Free Icons  |  Privacy  |   (c)   2011   

No comments:

Post a Comment