Group: microsoft.public.word.vba.general
From: Jay Freedman
Date: Thursday, February 28, 2008 7:13 PM
Subject: Re: HOW TO KEEP A RANGE INSIDE QUOTES?

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 FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.

On Thu, 28 Feb 2008 15:04:00 -0800, Elessvie
wrote:

>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