Determine if Selection is Text (Application Event Procedure)

J

Jon Borel

Hello,

I have a Word 2003 event handler that checks to see if the current
selection/cursor position is in text of the style "Bold Word." If so, the
event handler shows my "Bold Word" toolbar button as depressed. Note: The
"Bold Word" toolbar button runs a macro that toggles the bold style on & off.

' Class Module Private Sub below:

If Selection.Style = ActiveDocument.Styles("Bold Word") Then
CommandBars("myToolbar").Controls(1).State = msoButtonDown

Else
CommandBars("myToolbar").Controls(1).State = msoButtonUp

End If

It was working fine, until I selected a non-text object (say a text box or
image); it throws an error (e.g., "Object variable or With block variable not
set" or "Method not available because the object refers to a drawing
object"). So I tried to trap/filter any selection that wasn't text by
nesting the IF/THEN shown above in another IF/THEN:

If Selection.Type = wdSelectionNormal Then

If Selection.Style = ActiveDocument.Styles("Bold Word") Then
CommandBars("TCO Tools").Controls(1).State = msoButtonDown

Else
CommandBars("TCO Tools").Controls(1).State = msoButtonUp

End If

End If

This is working better (no errors) but now, because of the
"wdSelectionNormal" statement, I have to actually Select text (not just click
in a word). In other words, if I simply click in a word, the nested If/Then
doesn't fire, and nothing happens to my "Bold Word" toolbar button until I
select (hightlight) one or more characters.

When I click in (or select) a word, I want the macro to highlight my button
just like Word highlights the Bold button when you click/select a word that
is bold, and dims the bold button when you click/select non-bold text. (You
don't have to actually select/highlight text, just click in it.)

Thanks,
Jon
 
J

Jay Freedman

Hi Jon,

Modify the If statement to

If Selection.Type = wdSelectionNormal Or _
Selection.Type = wdSelectionIP Then

The wdSelectionIP value corresponds to a collapsed selection (insertion point),
while wdSelectionNormal corresponds to the selection of one or more characters.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.

On Tue, 30 Dec 2008 10:46:06 -0800, Jon Borel <Jon
 
J

Jon Borel

Jay,

PERFECT! Thanks for your help, it's great to have access to people like you
who are both willing and able to help.

Have a great day,
Jon
 

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