Font.Bold When True is not True

G

Greg Maxey

I learned something interesting today about the .Bold attribute (is that the
right term?)

I have some text in quotations "BOLDtext unboldtext" "Boldtext"
"Unboldtext"

I have a wildcard seach to look for text within quotation marks. I then
strip the quotaions marks and evaluate the remaining range. "IF" the range
(the whole range)is BOLD I want to do something.

Using this macro to illustrate, I learned the the statement:

If myrange.Font.Bold Then
Doesn't necessarily mean all the font in the range is bold. It appears to
mean that part of it or that not of it is not bolded.

Type "BOLD unbold" in a document with the quotation marks and BOLD word
bolded and run this illustration.

Sub ScratchMacro()
Dim myRange As Range
Set myRange = ActiveDocument.StoryRanges(wdMainTextStory)
With myRange.Find
'Find bold text in quotes
.Text = """*"""
.MatchWildcards = True
.Execute
'Strip quotation marks
myRange.Start = myRange.Start + 1
myRange.End = myRange.End - 1
myRange.Select 'You can see what is selected
If myRange.Font.Bold Then
MsgBox "Well not really true as part of the font isn't bold"
MsgBox myRange.Font.Bold 'Returns 999999 & " A -1 means really true"
End If
If myRange.Font.Bold <> -1 Then 'Or not true
MsgBox "Test Sat"
End If
End With
End Sub

So my lesson learned is that I can't take it for granted that statements
like
If whatever (thinking that means If whatever is true) Then
do something.
 
H

Helmut Weber

Hi Greg,

that's all all under control. "bold" is a property
which returns a long representing "true", "false" or
"wdundefined" with value 9999999. It does not return a boolean.

If range.font.bold means:
if there is something bold in the range then

Though I might have been trapped by this myself before.

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
G

Greg Maxey

Helmut,

Live and learn. Thanks.

Some of the ranges that occur in my finds contain hidden XE fields which are
not bold. For my purposes I need to confirm that the first and last
character in the visible range is bold. I am using:
If myRange.Characters.first.Font.Bold = True _
And myRange.Characters.last.Font.Bold = True Then
 

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