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 / August 2007

Tip: Looking for answers? Try searching our database.

Macro for creating a list of all highlighted words

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
KGollan - 15 Aug 2007 11:52 GMT
Hi,
Can anyone advise of a suitable macro for the following scenario:
A document has many words highlighted in different colours. I would like to
produce lists (in another document if possible) for each highlight colour, of
all the words that are highlighted with that colour.

Any advice would be appreciated.

Thanks,
Katy
Helmut Weber - 15 Aug 2007 12:48 GMT
Hi Katy,

>A document has many words highlighted in different colours.
>I would like to produce lists (in another document if possible)
>for each highlight colour,
>of all the words that are highlighted with that colour.

>Any advice would be appreciated.

if so,
something along these lines:

Sub Test5()
Dim rDcm As Range
Dim lClr As Long      ' color index
Dim Doc1 As Document
Dim Doc2 As Document
Set Doc1 = ActiveDocument
Set Doc2 = Documents.Add
Doc1.Activate
For lClr = 1 To 16
  Doc2.Range.InsertAfter "colorindex: " & lClr & vbCr
  Set rDcm = Doc1.Range
  With rDcm.Find
     .Highlight = True
     While .Execute
        If rDcm.HighlightColorIndex = lClr Then
           Doc2.Range.InsertAfter rDcm.Text & vbCr
        End If
     Wend
  End With
Next
End Sub

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

KGollan - 15 Aug 2007 13:56 GMT
Thanks Helmut - that worked perfectly!

> Hi Katy,
>
[quoted text clipped - 29 lines]
> Next
> End Sub
Graham Mayor - 15 Aug 2007 14:28 GMT
I had a similar plan, and your code was neater than mune, but I feel that it
would be more meaningful if real names were used eg

Sub ExtractHiLight()
Dim rDcm As Range
Dim lClr As Long      ' color index
Dim sCol As String
Dim Doc1 As Document
Dim Doc2 As Document
Set Doc1 = ActiveDocument
Set Doc2 = Documents.Add
Doc1.Activate
For lClr = 1 To 16
   Select Case lClr
       Case 1
           sCol = "Black"
       Case 2
           sCol = "Blue"
       Case 3
           sCol = "Turquoise"
       Case 4
           sCol = "Bright green"
       Case 5
           sCol = "Pink"
       Case 6
           sCol = "Red"
       Case 7
           sCol = "Yellow"
       Case 8
           sCol = "White"
       Case 9
           sCol = "Dark blue"
       Case 10
           sCol = "Teal"
       Case 11
           sCol = "Green"
       Case 12
           sCol = "Violet"
       Case 13
           sCol = "Dark red"
       Case 14
           sCol = "Dark yellow"
       Case 15
           sCol = "50% gray"
       Case 16
           sCol = "25% gray"
   End Select

Doc2.Range.InsertAfter sCol & vbCr
  Set rDcm = Doc1.Range
  With rDcm.Find
     .Highlight = True
     While .Execute
        If rDcm.HighlightColorIndex = lClr Then
           Doc2.Range.InsertAfter vbTab & rDcm.Text & vbCr
        End If
     Wend
  End With
Next
End Sub

I didn't get round to selectively inserting only the headers that were
actually used ;)

Signature

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> Hi Katy,
>
[quoted text clipped - 29 lines]
> Next
> End Sub
Helmut Weber - 15 Aug 2007 20:00 GMT
Hi Graham,

the result of your code looks nicer
than mine because of the additional vbtab.

Also, I didn't feel like finding out
what numerical equivalent each colorindex has.
I usually leave such things to the original poster.

>I didn't get round to selectively inserting
>only the headers that were actually used ;)

I don't think there is an elegant way to achieve that.
What possible ways there are you know as good as me,
and, certainly, you need no advice on that.

Schönen Abend noch.
(Have a nice evening.)

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Graham Mayor - 16 Aug 2007 07:04 GMT
Word 2007's vba help documents the number equivalents of the color index
table as I found when I tried to reverse engineer your use of the numbers,
so it was a simple cut and paste job ;) vba Help is somewhat improved over
the 2003 version.

I had been working on case statements using the colour names to achieve the
same ends but it produces a bulkier code which is why you got there first :)
I just adapted that to provide more descriptive headings.

I couldn't find an elegant way to selectively insert the headers either -
it's perhaps easier to remove the unwanted headers later, and that's what I
have done in my file copy.

Signature

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> Hi Graham,
>
[quoted text clipped - 14 lines]
> Schönen Abend noch.
> (Have a nice evening.)
Russ - 16 Aug 2007 07:22 GMT
Kgollan,

Adding FirstFind in Graham's subroutine may help it to output only found
color text headings.

Sub ExtractHiLight()
Dim FirstFind as Boolean
Dim rDcm As Range
Dim lClr As Long      ' color index
Dim sCol As String
Dim Doc1 As Document
Dim Doc2 As Document
If ActiveDocument.Content.HighlightColorIndex <> 0 then
Set Doc1 = ActiveDocument
Set Doc2 = Documents.Add
Doc1.Activate
For lClr = 1 To 16
   FirstFind = True
   Select Case lClr
       Case 1
           sCol = "Black"
       Case 2
           sCol = "Blue"
       Case 3
           sCol = "Turquoise"
       Case 4
           sCol = "Bright green"
       Case 5
           sCol = "Pink"
       Case 6
           sCol = "Red"
       Case 7
           sCol = "Yellow"
       Case 8
           sCol = "White"
       Case 9
           sCol = "Dark blue"
       Case 10
           sCol = "Teal"
       Case 11
           sCol = "Green"
       Case 12
           sCol = "Violet"
       Case 13
           sCol = "Dark red"
       Case 14
           sCol = "Dark yellow"
       Case 15
           sCol = "50% gray"
       Case 16
           sCol = "25% gray"
   End Select

  Set rDcm = Doc1.Range
  With rDcm.Find
     .Highlight = True
     While .Execute
        If rDcm.HighlightColorIndex = lClr Then
           If FirstFind then
               Doc2.Range.InsertAfter sCol & vbCr
               FirstFind = False
           End If
           Doc2.Range.InsertAfter vbTab & rDcm.Text & vbCr
        End If
     Wend
  End With
Next
Else
   MsgBox "No Highlighting found."
End If
End Sub

> I had a similar plan, and your code was neater than mune, but I feel that it
> would be more meaningful if real names were used eg
[quoted text clipped - 59 lines]
> I didn't get round to selectively inserting only the headers that were
> actually used ;)

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Graham Mayor - 16 Aug 2007 08:00 GMT
That works fine - FirstFind is a new one to me - and I could not find a
reference to it?

Signature

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> Kgollan,
>
[quoted text clipped - 131 lines]
>> I didn't get round to selectively inserting only the headers that
>> were actually used ;)
Russ - 16 Aug 2007 10:09 GMT
Graham,
I didn't mean to imply that it was anything special or was part of VBA
syntax. It was just a generic Boolean variable that I arbitrarily named to
keep track of the first time through that piece of code for a color. It gets
toggled to False the first time through and it gets reset to True when the
number changes for the next color. It is just a programming technique, in
other words.

> That works fine - FirstFind is a new one to me - and I could not find a
> reference to it?

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Graham Mayor - 16 Aug 2007 14:06 GMT
That'll teach me to read the code more thoroughly before responding ;)

Signature

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> Graham,
> I didn't mean to imply that it was anything special or was part of VBA
[quoted text clipped - 6 lines]
>> That works fine - FirstFind is a new one to me - and I could not
>> find a reference to it?
Russ - 16 Aug 2007 18:20 GMT
I'm afraid I'm guilty of that. Something will 'pop out' when I read a post
that I've dealt with recently, and the other relevant details, I seem to
skip over. In a rush to respond, often it is not until after I send a reply,
that I notice what I responded to, was out of context.
> That'll teach me to read the code more thoroughly before responding ;)

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Michael Bednarek - 16 Aug 2007 12:45 GMT
>That works fine - FirstFind is a new one to me - and I could not find a
>reference to it?

It's a local variable which Russ introduced; see snippets below:

>> Adding FirstFind in Graham's subroutine may help it to output only
>> found color text headings.
>>
>> Sub ExtractHiLight()
>> Dim FirstFind as Boolean
[snip]
>>    FirstFind = True
[snip]
>>            If FirstFind then
>>                Doc2.Range.InsertAfter sCol & vbCr
>>                FirstFind = False
>>            End If
[snip]

Signature

Michael Bednarek   http://mbednarek.com/   "POST NO BILLS"

Graham Mayor - 16 Aug 2007 14:09 GMT
Yes - foolish me, I missed the relevance. I was in a hurry to go out and
responded without reading the code properly :(

Signature

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

>> That works fine - FirstFind is a new one to me - and I could not
>> find a reference to it?
[quoted text clipped - 14 lines]
>>>            End If
> [snip]
Helmut Weber - 17 Aug 2007 02:23 GMT
Hi Graham,

> That works fine - FirstFind is a new one to me - and I could not find a
> reference to it?

LOL

strange, I've been making the same mistake  you made,
searching the groups and help for "Firstfind" for half an hour
and didn't find it. :-)

Signature

Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000 (german versions)

Graham Mayor - 17 Aug 2007 09:59 GMT
I mean this as no criticism of Russ who is a much better programmer than me,
and from whom I have learned a lot, but the reason I overlooked it has much
to do with different programming styles. I tend to use an initial to
identify variables eg for boolean I would have called it bFirstfind.
Firstfind just seemed so right as a vba statement :)

Signature

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> Hi Graham,
>
[quoted text clipped - 6 lines]
> searching the groups and help for "Firstfind" for half an hour
> and didn't find it. :-)
Russ - 17 Aug 2007 19:08 GMT
Yes, I have strayed away from prefixing variables, probably in an effort to
save keystrokes when typing. I'm lazy, I admit it. I'll try to remember to
use them again, because it will even help me when I look at my own code
after being away from it for awhile. Thanks for the reminder.

Graham, I would also like to take this opportunity to thank you for how much
I have learn about using Word and programming from your website. You are
truly doing a great service for everyone.

> I mean this as no criticism of Russ who is a much better programmer than me,
> and from whom I have learned a lot, but the reason I overlooked it has much
> to do with different programming styles. I tend to use an initial to
> identify variables eg for boolean I would have called it bFirstfind.
> Firstfind just seemed so right as a vba statement :)

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Graham Mayor - 18 Aug 2007 08:51 GMT
Thanks for the kind comments. With the background of a technical writer I
can explain things I understand better than I can work them out. For that I
tend to leave it to people like yourself who can produce much tighter code.
My site is just a place where the good ideas are pulled together. I always
credit those whose code I 'borrow'. ;)

I hope I can keep the web site going, however the running costs are rising
because of the traffic. The site gets over 2000 hits a day. At the moment
Google's AdSense is discreet and covers the costs, but for how much longer
..... I don't want to resort to more advertising or sponsorship, and the
logistics of moving the site to a cheaper service provider are for the
moment filed in the 'too hard to do' basket :(

Signature

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> Yes, I have strayed away from prefixing variables, probably in an
> effort to save keystrokes when typing. I'm lazy, I admit it. I'll try
[quoted text clipped - 12 lines]
>> have called it bFirstfind. Firstfind just seemed so right as a vba
>> statement :)
 
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.