Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / September 2007

Tip: Looking for answers? Try searching our database.

how to highlight found word?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Geoff Cox < - 26 Aug 2007 15:01 GMT
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
Shauna Kelly - 26 Aug 2007 15:34 GMT
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

> Hello
>
[quoted text clipped - 11 lines]
>
> End With
Geoff Cox < - 27 Aug 2007 15:41 GMT
>Hi
>
[quoted text clipped - 11 lines]
>
>Hope this helps.

Thanks Shauna.

Cheers

Geoff

>Shauna Kelly.  Microsoft MVP.
>http://www.shaunakelly.com/word
[quoted text clipped - 14 lines]
>>
>> End With
Geoff Cox < - 27 Aug 2007 15:48 GMT
>Hi
>
[quoted text clipped - 9 lines]
>    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

>Hope this helps.
>
[quoted text clipped - 16 lines]
>>
>> End With
Helmut Weber - 27 Aug 2007 16:32 GMT
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

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Geoff Cox < - 27 Aug 2007 18:20 GMT
>Hi Geoff,
>
[quoted text clipped - 8 lines]
>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?!
Shauna Kelly - 27 Aug 2007 22:57 GMT
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

>>Hi Geoff,
>>
[quoted text clipped - 20 lines]
> PS any suggestions as to where I might find answers to really basic
> VBA questions - without troubling kind souls such as yourself?!
Geoff Cox < - 28 Aug 2007 07:08 GMT
>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

>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
[quoted text clipped - 37 lines]
>> PS any suggestions as to where I might find answers to really basic
>> VBA questions - without troubling kind souls such as yourself?!
Helmut Weber - 29 Aug 2007 16:12 GMT
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.

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Geoff Cox - 29 Aug 2007 18:28 GMT
>Hi Geoff,
>
[quoted text clipped - 7 lines]
>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
Helmut Weber - 30 Aug 2007 00:13 GMT
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

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Geoff Cox - 30 Aug 2007 07:44 GMT
>Hi Geoff,
>
[quoted text clipped - 13 lines]
>End With
>End Sub

thanks Helmut - trying it out now.

Cheers

Geoff
Geoff Cox - 30 Aug 2007 07:56 GMT
>Hi Geoff,
>
[quoted text clipped - 13 lines]
>End With
>End Sub

Helmut,

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

Cheers

Geoff
Russ - 30 Aug 2007 09:58 GMT
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.

>> Hi Geoff,
>>
[quoted text clipped - 22 lines]
>
> Geoff

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Helmut Weber - 30 Aug 2007 15:31 GMT
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

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Geoff Cox - 31 Aug 2007 07:56 GMT
>Hi everybody,
>
[quoted text clipped - 19 lines]
>End With
>End Sub

fine - thanks Helmut.

Cheers

Geoff
Shauna Kelly - 30 Aug 2007 00:20 GMT
Hi Geoff

>> a paragraph
>>which contains nothing but a red word is
>>preceded by an empty paragraph and
>>followed by an empty paragraph.

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

>>Hi Geoff,
>>
[quoted text clipped - 23 lines]
>
> Geoff
Geoff Cox - 30 Aug 2007 07:43 GMT
>Hi Geoff
>
[quoted text clipped - 6 lines]
>*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

>Note that this will:
>- not find the very first paragraph in the document (because it has no
[quoted text clipped - 88 lines]
>>
>> Geoff
Geoff Cox < - 01 Sep 2007 21:47 GMT
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

>Public Sub MakeHeadingsFromRedText()
>
[quoted text clipped - 73 lines]
>>
>> Geoff
Geoff Cox < - 01 Sep 2007 21:59 GMT
>Hi Geoff
>
[quoted text clipped - 6 lines]
>*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

>Note that this will:
>- not find the very first paragraph in the document (because it has no
[quoted text clipped - 88 lines]
>>
>> Geoff
Doug Robbins - Word MVP - 01 Sep 2007 22:52 GMT
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

Signature

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
>>
[quoted text clipped - 114 lines]
>>>
>>> Geoff
Geoff Cox < - 02 Sep 2007 08:42 GMT
>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

>Dim oPara As Word.Paragraph
>Dim paraPrevious As Word.Paragraph
[quoted text clipped - 48 lines]
>    End If
>Next oPara
Helmut Weber - 02 Sep 2007 10:57 GMT
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

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Geoff Cox < - 03 Sep 2007 11:54 GMT
>Hi Geoff,
>
>getting more and more complicated.

I know but I am OK now - many thanks!

Cheers

Geoff

>Sub Macro2bbb()
>Dim oRngD As Range     ' document's range
[quoted text clipped - 16 lines]
>End With
>End Sub
Greg Maxey - 26 Aug 2007 15:40 GMT
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

Signature

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

> Hello
>
[quoted text clipped - 11 lines]
>
> End With
Geoff Cox < - 27 Aug 2007 15:42 GMT
>Wouldn't your code find "Green" ;-)

too true! at least someone is awake!

Cheers

Geoff

>Sub Test()
>Dim oRng As Word.Range
[quoted text clipped - 7 lines]
>End With
>End Sub
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.