Macro for highlighting

A

Anthony Giorgianni

Hello Everyone

In my old version (2000?) of work I was able to set up macros to turn on
highlighting of different colors- having toolbar macros called "Green"
"yellow" "red"etc.

But I can't seem to do it in word 2003. I've tried importing my old macros
and rerecroding as well. It just won't turn on the highlighting. Any ideas?

Thanks.

Regards,

Anthony Giorgianni
For everyone's benefit, please reply to the group.
 
A

Anthony Giorgianni

Yes, Tony

Here is what the recorder did when I tried to set it up.

Sub Green()
'
' Green Macro
' Macro recorded 1/31/2007 by
'
Options.DefaultHighlightColorIndex = wdBrightGreen
End Sub

It doesn't work to activate green highlighting; nor does it turn selected
text green.
here is one that definitely ran on office 2000 that doesn't work on Word
2003. It actually brings up "find and replace," oddly, as do all my other
old highlighter macros!

Sub Red()
'
' Red Macro
' Macro recorded 01/06/2004 by Anthony Giorgianni
'
Options.DefaultHighlightColorIndex = wdRed
SendKeys "{F5}"
End Sub

Regards,
Anthony Giorgianni

For everyone's benefit, please reply to the group.
 
T

Tony Jollans

The Macro recorder, unfortunately, is not perfect. All that does is set the
default highlight colour; to apply it to the selection you need:

Selection.Range.HighlightColorIndex = wdGreen

You don't actually need to set the default colour; the above line is all you
need and it will highlight the selcted text in the given colour (green in
this example).

I don't understand the Find and Replace bit, sorry.
 
G

Guest

The following macro will change the highlight color of the selection to red -
it does not change the color selected in the Highlight icon in the Formatting
toolbar:

Sub HighLight_Red()
Selection.Range.HighlightColorIndex = wdRed
End Sub

If you want to change the color selected in the Highlight icon too, add the
following code line to the macro:

Options.DefaultHighlightColorIndex = wdRed

Replace wdRed by the desired WdColorIndex.

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
G

Guest

I somehow failed to notice that the answer in my post above was already given
by Tony Jollans. Sorry.

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
G

Guest

Thanks, Tony. Anyway, two similar answers are better than none ;-)

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
A

Anthony Giorgianni

Thank you both. Works well. Now to finish it, how do I just turn on
highlighting tool with the appropriate color so that I can go highlighting
things without selecting them first?

Regards,
Anthony Giorgianni

For everyone's benefit, please reply to the group.
 
T

Tony Jollans

Not sure about not selecting them first but the recorded code you posted
should change the default on the toolbar button
 
A

Anthony Giorgianni

Thanks Tony.

I found the problem. On my old laptop, I must have assigned F5 to turn on
hgihlighting. So when I transferred the macro to my new laptop with
"Sendkey" F5, it didn't trigger the highlighter but instead ran Find and
replace, the assignment for f5 on that machine :O)

Now it works fine with:

Sub HighLight_Red()
Selection.Range.HighlightColorIndex = wdRed
SendKeys "(F10)"

With F10 now assigned to turn on highlighting.

End Sub

Thanks again to you and Lene.

Regards,
Anthony Giorgianni

For everyone's benefit, please reply to the group.
 
A

Anthony Giorgianni

Oooops .... make that

Sub Highlight red()

Options.DefaultHighlightColorIndex = wdRed
SendKeys "{F10}"
End Sub

This turns on highlighting to highlight unselected text or highlights
selected text.

Thanks again.

Regards,
Anthony Giorgianni

For everyone's benefit, please reply to the group.
 
T

Tony Jollans

Whilst that may work for you ...

* most people don't have F10 - or any key - assigned to highlighting
* Sendkeys is generally best avoided

If you use the explicit statement (Selection.Range.HighlightColorIndex =
wdRed) that Lene and I have both suggested you will find it works just as
well. The only difference (which you'll generally be unaware of) is that it
will work when Sendkeys would have failed, and also that it will work for
other people as well.
 
A

Anthony Giorgianni

Thanks Tony

But without using Sendkey, I can't figure out how simply to turn on
highlighting without selecting anything first. Am I missing something?


--
Regards,
Anthony Giorgianni

The return address for this post is fictitious. Please reply by posting back
to the newsgroup.
 
T

Tony Jollans

There are two (VBA) statements that you seem to be confusing or
misunderstanding:

1. Selection.Range.HighlightColorIndex = wdRed

This highlights the selection without affecting the toolbar button - and is
the one to use to avoid sendkeys.

2. Options.DefaultHighlightColorIndex = wdRed

This changes the toolbar button (which then allows the sendkeys to use it to
get the chosen colour) but does not affect the text.
 
A

Anthony Giorgianni

Thanks Tony

You're right. I did not understand that difference between the two lines.
But I'm still not certain.

What then would be the sequence to create a taskbar button (say a red box,
for example) that, when clicked, would

1) Change the highlight button to red
2) Turn on red highlighting so that:
a) Highlighted text would turn red or
b) if no text is highlighted, simply turn on red highlighting so
that I can highlight as I read by dragging my cursor across unhighlighted
text?

Right now I'm using:

1) Options.DefaultHighlightColorIndex = wdRed (to select red and turn any
selected text to red)
2) SendKeys "{F10}" (to turn on highlighting - where my computer has been
set to use F10 to turn on highlighting).

I think you are right. It would be better not to use the sendkeys command
(especially if I want to move the macro to another computer). But I'm not
sure of the sequence that will avoid using sendkeys.

Sorry if I'm being obtuse. I'm not very familiar with VB.

Thanks.

Regards,
Anthony Giorgianni

For everyone's benefit, please reply to the group.
 
G

Guest

Anthony,

You only need to combine the two lines of code as in the macro below:

Sub HighLight_Red()
Options.DefaultHighlightColorIndex = wdRed
Selection.Range.HighlightColorIndex = wdRed
End Sub

The first code line changes the highlight button to red.
The second line will apply red highlight to the selection. If no text is
selected, the second line does not make any changes to the text.

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
G

Greg Maxey

Anthony,

You only need to combine the two lines of code as in the macro below:

Sub HighLight_Red()
Options.DefaultHighlightColorIndex = wdRed
Selection.Range.HighlightColorIndex = wdRed
End Sub

The first code line changes the highlight button to red.
The second line will apply red highlight to the selection. If no text is
selected, the second line does not make any changes to the text.

--
Regards
Lene Fredborg
DocTools - Denmarkwww.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word














- Show quoted text -

Lene,

I think he wanted to change in previous highlighting to red as well.
Also (this could be due to my Tools>Options>Edit settings) if the IP
is withing a word, even if that word is not selected, it would be
highlighted.

Try:

Sub Scratchmacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Content
Options.DefaultHighlightColorIndex = wdRed
With oRng.Find
.Highlight = True
While .Execute
oRng.HighlightColorIndex = Options.DefaultHighlightColorIndex
Wend
End With
If Selection.Range.Start <> Selection.Range.End Then
Selection.Range.HighlightColorIndex = wdRed
End If
End Sub

AFAIK, this is as close to your ideal as you can get with VBA. You
could then use the format painter tool to continue hightlight text.
 
G

Guest

Greg,

About changing the previous highlighting too: after reading the description
again now, I am sure you are right - however, I did not read it that way
first.

About the insertion point being in a word: you are right, if the option
“When selecting, automatically select entire word†in Tools > Options > Edit
tab is turned on, the code line "Selection.Range.HighlightColorIndex = wdRed"
would highlight that word. I should have taken this into consideration by
making a check in the code as you did in your version - unfortunately, I
failed to notice this because I do not have that option turned on…

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
A

Anthony Giorgianni

Thanks everyone for your help.

I think I'll simply to stick with using the sendkeys command. That's a neat
macro Lene but doesn't seem to do what I'm looking for. I think I'm probably
not explaining it right. All I want to be able to do is simply turn on
highlighting with a certain color with one click, avoiding having to go up
to the highlighting menu item, select the color and turn it on.

The reason is that, as a journalist, many times I'm reading a document and
want to highlight items for different importance - green = interesting but
not critical, yellow = pretty important, red = information that I must use.
So when I see something pretty important, I want to turn on yellow
highlighting with one click of a toolbar icon. Then I highlight that
(usually without selecting it first) Later I might see something that's
critical, so I want to turn on red highlighting with one click.

This does that exactly

Options.DefaultHighlightColorIndex = wdYellow REM: Selects the
highlighting color yellow
SendKeys "{F5}" REM Turns on highlighting, where Word has been set to
associate F5 with turning on highlighting.

Tony suggested that using sendkeys isn't the best way to do this. It
certainly doesn't make the macro portable if I want to transfer it to
another machine without having to set F5 (as I found out when I tried to
transfer it from my home to my work machine and forgot about setting F5 to
turn on highlighting.) So I was curious if there was another way to do it.
But it's no big deal. It works fine. By the way, to make it really easy, I
created a "highlighting" toolbar with icons that are simply colored squares,
each assigned to a different color macro and one assigned to "no color" for
clearing.

Thanks again.

Regards
Anthony Giorgianni

For everyone's benefit, please reply to the group.
 

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