Find & Replace Question

V

Vince

Hi all,
Here's what I need to be able to do:

Sample Text:
Mary had a little lamb, little lamb, little lamb; Mary had a little lamb
(forgot what comes after)

Now, assuming that all the 'lamb's are in Bold, I need to Find and Replace
all Bold words and tag them appropriately.

So, the answer would be:
Mary had a little <BOLD>lamb</BOLD>, little <BOLD>lamb</BOLD>, little
<BOLD>lamb</BOLD>; Mary had a little <BOLD>lamb</BOLD> (forgot what comes
after)

Now, if I find and Replace and use Bold Formatting, the original text is
taken text away, leaving me with this:
Mary had a little <BOLD></BOLD>, little <BOLD></BOLD>, little <BOLD></BOLD>;
Mary had a little <BOLD></BOLD> (forgot what comes after)

So, how do you think I should be able to code this? Please don't tell me to
go through the text, word by word...

Thank you for your suggestions and for reading.
Vince
 
V

Vince

Never mind!!! I figured it out!! It goes:

Find : Formatting BOLD
Replace with: This thingo below
<BOLD>^&</BOLD>

and why didn't I think of this earlier!!

Vince
 
H

Helmut Weber

Hi Vince,
like this:
Sub Resetsearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub
Sub Test321()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
Resetsearch
With rDcm.Find
.Text = ""
.Font.Bold = True
.MatchWholeWord = True
.Replacement.Text = "<BOLD>lamb</BOLD>"
.Replacement.Font.Bold = False
.Execute Replace:=wdReplaceAll
End With
Resetsearch
End Sub
---
Making the replacement text not bold is
advisable. Otherwise you may end up with
<BOLD><BOLD>lamb</BOLD></BOLD> etc.
for each run of the macro. Though
..MatchWholeWord = True takes care of that
problem here. But would not take care,
if you wanted to replace something like
" in this moment ", possibly preceeded and
followed by bold spaces.
 
H

Helmut Weber

Hi,
improved version, though replacing all
bold text by "lamb" was obviously not meant
seriously.
Typing too fast. I'm ashamed.
Sub Test321()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
Resetsearch
With rDcm.Find
.Text = ""
.Font.Bold = True
.MatchWholeWord = True
.MatchWildcards = True
.Replacement.Text = "<BOLD>^&</BOLD>"
.Replacement.Font.Bold = False
.Execute Replace:=wdReplaceAll
End With
Resetsearch
End Sub
 

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