Word 2000: Underline a word underlines space between

V

VBA Coder

I have the following code to select a Range object and apply underlining to
a particular word within the range. For some reason, Word is not only
underlining the word, but the space after the word. Does anyone know how I
can get it to only underline the word?

Dim rRange As Range
Set rRange = ActiveDocument.Bookmarks("MyRange").Range
rRange.Select
With rRange
' Underline the following word within the range
.Words(53).Font.Underline = wdUnderlineSingle
End With
 
J

JGM

Hi VBA Coder

A word for Word always includes the following space.

Use this instead:
'_______________________________________
Dim rRange As Range

Set rRange = ActiveDocument.Bookmarks("MyRange").Range.Words(53)

rRange.SetRange rRange.Start, rRange.End - 1
rRange.Font.Underline = wdUnderlineSingle
'_______________________________________

You do not need to select a range to cgage some of its attributes.
If you need rRange later in the code, then add this line after the above:

Set rRange = ActiveDocument.Bookmarks("MyRange").Range

HTH
Cheers
 
V

VBA Coder

Thank you, that solved it.

But I did notice one very strange thing, if my Range of words contains "("
or ")", VBA thinks that the parenthesis are a word. ie: If my string is
"(This is a Test)", VBA thinks there are 6 words, not 4. Very odd.
 
W

Word Heretic

G'day "VBA Coder" <[email protected]_SPAM.>,

it aint odd when you consider that MS had to make some rules as to
what is a word and what aint - with hardcoded selections from an
almost infinite arrangement of grammatical constructions - not all
correct natch, but USED!).

Have a few bottles of red whilst doing nothing but thinking what is a
word and what isnt for most user scenarios and you'll see that
including trailing punctuation and spaces is a NEAT PROGRAMMATICAL way
of dealing with natural language clustering.

If our preference was for preceding spaces, it would prob be
reversed...

Hmmm???



VBA Coder said:
Thank you, that solved it.

But I did notice one very strange thing, if my Range of words contains "("
or ")", VBA thinks that the parenthesis are a word. ie: If my string is
"(This is a Test)", VBA thinks there are 6 words, not 4. Very odd.

Steve Hudson

Word Heretic, Sydney, Australia
Tricky stuff with Word or words for you.
wordheretic.com

If my answers r 2 terse, ask again or hassle an MVP,
at least they get recognition for it then.
Lengthy replies offlist require payment.
 

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