Assessment

G

Greg

Hi,

The other day a person asked how to find and underline text that was quoted
in a document without also underlining the quotation marks. I offered a
double pass find and replace solution which works, but I became curious as
how to do it with a macro. I am a VBA novice and rusty now from lack of us.
I put together the following code which seems to work, but I had difficulties
resolving how to find each occurence of text without creating a circular
loop. If I used .Wrap wdFindContinous, it would get in a never ending loop.
If I used .Wrap FindStop, then I only found the first occurrence. I recalled
from earlier experience that I could use a .Collaspe wdCollaspeEnd to force
the search to continue until reaching the end of the document. I think that
there should be a better way, but stuck with what I have. Any insight
appreciated.


Sub FindAndUnderlineTextInQuotes()

'Underlines text exclusive of the quotes marks

Set oRng = ActiveDocument.Content

With oRng.Find
.ClearFormatting
.Text = """<*>"""
.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
 
J

Jay Freedman

Hi Greg,

No, there isn't a better way. When you need to stop at each find and
manipulate the range or the found text, then you have to use wdFindStop and
collapse the range before starting the next loop iteration (and you may also
have to move the collapsed range past the end of the original find, although
that doesn't seem to be necessary in this case).

The only comment I have on your code is that the statement "If
oRng.Find.Found = True Then" isn't necessary -- using the implicit return
value of the .Execute method in the Do While guarantees that .Found is true
whenever you enter the loop body.
 
G

Greg

Jay,

Thanks for the look and your comments regarding the extraneous code.
Between my post and your answer, I did stumble on another method. It
searches for text not underlined and then uses the method wdFindContinue. I
don't suppose that it is any great improvement, but posted here for you to
see:

Sub FindAndUnderlineTextInQuotes()

'Underlines text contained in quotation marks exclusive of the quotes marks
Dim oRng As Range
Set oRng = ActiveDocument.Content

With oRng.Find
.ClearFormatting
.Font.Underline = wdUnderlineNone
.Text = """<*>"""
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
Do While .Execute
With oRng
.MoveEnd Unit:=wdCharacter, Count:=-1
.MoveStart Unit:=wdCharacter, Count:=1
.Font.Underline = wdUnderlineSingle
End With
Loop
End With
End Sub
 
J

Jay Freedman

Hi Greg,

There are always at least two or three ways to do anything in Word. :)
Sometimes it's harder to decide which way to go than to find more ways...

In this case, there's a small difference in the internal behavior of the two
macros after the last occurrence is found (but no difference in the
resulting document):

- In the macro that uses wdFindStop, the .Execute searches from the current
position of oRng to the end of the document, finds nothing, and returns
False.

- In the macro that uses wdFindContinue, the .Execute searches from the
current position of oRng to the end of the document, wraps around to the
beginning, and continues to search until it reaches oRng "from behind".
Finding no occurrences that aren't already underlined, it also returns
False.

If the document is very long, it will take the second macro slightly more
time to finish this last search. It probably wouldn't be noticeable in most
documents.
 

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