HOW TO KEEP A RANGE INSIDE QUOTES?

E

Elessvie

Hi, programmers,

I have a question on a very helpful macro posted here for finding and
underlining text between quotation marks. It works great--except when quotes
are preceded or followed by a comma or Wildcard.

In that case, the range extends right on through to the next instance of
quotes that DON’T have any commas right before or after them.

Is there a way to keep the range from extending in these instances? Thank
you for all your help.

Here is the code:

Sub FindAndUnderlineTextInQuotes()
Dim oRng As Range
Set oRng = ActiveDocument.Content
With oRng.Find
.ClearFormatting
.Text = "[^0034^0147]<*>[^0034^0148]"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
Do While .Execute
If oRng.Find.Found = True Then
With oRng
.MoveEnd Unit:=wdCharacter, Count:=-1
.MoveStart Unit:=wdCharacter, Count:=1
.Font.Underline = True
.Collapse wdCollapseEnd
End With
End If
Loop
End With
End Sub


Thanks!

-Lynne
 
J

Jay Freedman

The problem you describe is caused by the angle brackets around the asterisk in
the middle of the search expression. It specifies that in order to get a match,
the stuff between the quote marks _must_ start at the beginning of a word and
end at the end of a word. When you get a comma (or, for that matter, any
punctuation or space) before the closing quote, that isn't recognized as a
match. However, as Word continues to extend the search range, it may eventually
find a quote mark at the end of a word, which it takes as the end of the match.
The solution is simply to remove the angle brackets, letting the asterisk match
any sequence of characters.

There's another problem with the code. The .Collapse near the end of the Do loop
leaves the oRng to the left of the ending quote mark (because of the preceding
..MoveEnd statement), so that's where the next search is going to start. You need
to move oRng to the right of that quote mark.

A minor nit-pick is that the If oRng.Find.Found test isn't needed -- whenever
..Execute returns True, that automatically makes oRng.Find.Found = True also.

Here's the fixed-up version:

Sub FindAndUnderlineTextInQuotes()
Dim oRng As Range
Set oRng = ActiveDocument.Content
With oRng.Find
.ClearFormatting
.Text = "[^0034^0147]*[^0034^0148]"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
Do While .Execute
With oRng
.MoveEnd Unit:=wdCharacter, Count:=-1
.MoveStart Unit:=wdCharacter, Count:=1
.Font.Underline = True
.Move Unit:=wdCharacter, Count:=2
End With
Loop
End With
End Sub

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

Elessvie

Thank you, once again, so much, for all three points!

I always learn a lot from this board, whether posting directly or searching
subjects.

-Lynne


Jay Freedman said:
The problem you describe is caused by the angle brackets around the asterisk in
the middle of the search expression. It specifies that in order to get a match,
the stuff between the quote marks _must_ start at the beginning of a word and
end at the end of a word. When you get a comma (or, for that matter, any
punctuation or space) before the closing quote, that isn't recognized as a
match. However, as Word continues to extend the search range, it may eventually
find a quote mark at the end of a word, which it takes as the end of the match.
The solution is simply to remove the angle brackets, letting the asterisk match
any sequence of characters.

There's another problem with the code. The .Collapse near the end of the Do loop
leaves the oRng to the left of the ending quote mark (because of the preceding
..MoveEnd statement), so that's where the next search is going to start. You need
to move oRng to the right of that quote mark.

A minor nit-pick is that the If oRng.Find.Found test isn't needed -- whenever
..Execute returns True, that automatically makes oRng.Find.Found = True also.

Here's the fixed-up version:

Sub FindAndUnderlineTextInQuotes()
Dim oRng As Range
Set oRng = ActiveDocument.Content
With oRng.Find
.ClearFormatting
.Text = "[^0034^0147]*[^0034^0148]"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
Do While .Execute
With oRng
.MoveEnd Unit:=wdCharacter, Count:=-1
.MoveStart Unit:=wdCharacter, Count:=1
.Font.Underline = True
.Move Unit:=wdCharacter, Count:=2
End With
Loop
End With
End Sub

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

Hi, programmers,

I have a question on a very helpful macro posted here for finding and
underlining text between quotation marks. It works great--except when quotes
are preceded or followed by a comma or Wildcard.

In that case, the range extends right on through to the next instance of
quotes that DON’T have any commas right before or after them.

Is there a way to keep the range from extending in these instances? Thank
you for all your help.

Here is the code:

Sub FindAndUnderlineTextInQuotes()
Dim oRng As Range
Set oRng = ActiveDocument.Content
With oRng.Find
.ClearFormatting
.Text = "[^0034^0147]<*>[^0034^0148]"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
Do While .Execute
If oRng.Find.Found = True Then
With oRng
.MoveEnd Unit:=wdCharacter, Count:=-1
.MoveStart Unit:=wdCharacter, Count:=1
.Font.Underline = True
.Collapse wdCollapseEnd
End With
End If
Loop
End With
End Sub


Thanks!

-Lynne
 

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