find and replace bug--

A

Aaron van de Sande

I am trying to replace the word formatting with html formatting in
document using find and replace. I know there are much better ways to
do this, but I am supporting an old application architected by someone
else.

Here is the block of code--

r.Find.ClearFormatting
r.Find.Font.bold = True
With r.Find
.text = ""
.Replacement.text = "<b>^&</b>"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
r.Find.Execute Replace:=wdReplaceAll

It seems to work fine, but Word goes into an endless loop when it
encounters a URL. It is possible to replicate using the find and
replace dialog boxes in the word environment.
Any help in this matter would be appreciated.
 
L

Larry

I don't know how to fix your code, but I'll show you what I use which
works. Instead of doing a straight search and replace, I run a loop in
which I find the bold text, then retract the selection from any spaces
and punctuation so that only the text gets the tags.

Larry



' make hyperlinks unbold so they won't get bold tags.
ActiveDocument.Styles("Hyperlink").BaseStyle = "Default Paragraph
Font"
With ActiveDocument.Styles("Hyperlink").Font
.Underline = wdUnderlineSingle
.ColorIndex = wdPink
.Bold = False
End With


Selection.HomeKey wdStory

' Put bold tags around bold text.
With Selection.Find: .ClearFormatting: .Text = "": .Font.Bold = True: _
..Forward = True: .Wrap = wdFindStop
Do While .Execute
With Selection
' retract selection from spaces and punctuation
.MoveEndWhile cset:=" " & "." & "?" & "!" & "," & _
"""", Count:=-6
.InsertBefore "<b>": .InsertAfter "</b>": .MoveRight wdWord, 1: End
With
Loop
End With
 

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