Problem selecting words in document

M

Mark F.

The following code is supposed to color all the quoted text brown. When
the first double quotes character is found, the text is colored from
that point on! I am having difficulty understanding the parameters for
the various "Selection" objects and how their parameters act on the
document's selection.

' Begin code ==========================
Public Function ColorizeQuotedText()
Dim nWordPos As Integer
On Error Resume Next
With ActiveDocument.Words

For nWordPos = 1 To .Count
If .Item(nWordPos).Characters.First.Text = """" Then
.Item(nWordPos).Select

' Bug! ==================================
Selection.Extend character:=""""
Selection.MoveLeft Unit:=wdWord, Count:=-1,
Extend:=wdExtend
' =======================================

Selection.Find.Format = True
Selection.Font.Color = wdColorBrown
End If
Next nWordPos
End With
End Function
' =====================================

Thanks in advance for your help!
(c:
 
P

Peter Hewett

Hi Mark

The easy way (if you're familiar with it) is to use the Find object (Words
Search + Replace). The following code finds all "text"
(doublequote,text,doublequote) in your document and sets the font colour of
the text inside the doublequotes to Brown. Word usually uses "Smart
Quotes" so I've setup the code for them rather than the normal doublequotes
you'd enter through the VBA IDE editor:

Sub SAndRX()
Dim rngReplace As Word.Range
Dim rngFound As Word.Range

Set rngReplace = ActiveDocument.Content
With rngReplace.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "“*”"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True

' Find all occurrences in the document
Do While .Execute

' Text inside smart quotes is Brown
Set rngFound = rngReplace.Duplicate
rngFound.MoveStart wdCharacter, 1
rngFound.MoveEnd wdCharacter, -1
rngFound.Font.Color = wdColorBrown

' Setup range to continue the search after
' the text that we just found
rngReplace.Collapse wdCollapseEnd
Loop
End With
End Sub

HTH + Cheers - Peter
 
P

Peter Hewett

Hi Mark

I just viewed the post and you can't see the special "Smart Quote"
characters. But if you copy the code and paste it into the VBA Editor the
correct characters will be there!

HTH + Cheers - Peter
 
M

Mark F.

Peter Hewett said:
Hi Mark

I just viewed the post and you can't see the special "Smart Quote"
characters. But if you copy the code and paste it into the VBA Editor the
correct characters will be there!

HTH + Cheers - Peter
Thanks Peter! I will give it a try. The MS help files do not go into a
lot of detail about some of the parameters for the various objects, so I
appreciate the help.
(c:
 

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