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
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