Find Text, Replace Text and Bold It in a Macro

J

jerem

Hi,

Trying to create a macro through keystrokes that does this:

finds text: bbb*bbb, Wildcards on
replaces text: ^& (with whatever it found)
then bold the replacement

Seems simple enough to me and when I do the keystrokes while recording it
does exactly what I want it to do, but it won't do it when I run the macro.
This is the code that gets recorded:

Sub Macro2()
'
'
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "bbb*bbb"
.Replacement.Text = "^&"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

I've fooled around with placing the statement: Selection.Font.Bold =
wdToggle in various spots, but that doesn't work either.

Any ideas, suggestions, etc.

Thanks in advance.
 
G

Greg Maxey

Sub ScratchMaco()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Text = "bbb*bbb"
.MatchWildcards = True
With .Replacement
.Text = "^&"
.Font.Bold = True
End With
.Execute Replace:=wdReplaceAll
End With
End Sub


Hi,

Trying to create a macro through keystrokes that does this:

finds text: bbb*bbb, Wildcards on
replaces text: ^& (with whatever it found)
then bold the replacement

Seems simple enough to me and when I do the keystrokes while
recording it does exactly what I want it to do, but it won't do it
when I run the macro. This is the code that gets recorded:

Sub Macro2()
'
'
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "bbb*bbb"
.Replacement.Text = "^&"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

I've fooled around with placing the statement: Selection.Font.Bold =
wdToggle in various spots, but that doesn't work either.

Any ideas, suggestions, etc.

Thanks in advance.

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org


McCain/Palin '08 !!!
 
J

jerem

Works great Greg. Thanks. Can I ask you another question - can you tell me
what the coding is for one macro to call another?
 
G

Graham Mayor

Call it by name eg

Sub Macro1()
Macro2
End Sub

Sub Macro2()
MsgBox "this is macro 2"
End Sub


However if you are planning to run a series of replacements, there are
probably more elegant ways to achieve that from the one macro.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Greg Maxey

Jerem,

That's two questions ;-). Yes, you can ask another. Graham ansewered the
second.

I won't say it's elegant, but to find multiple terms you can define an array
and look for each member:

Sub ScrachMacro()
Dim SearchArray As Variant
Dim myRange As Range
Dim i As Long
Dim TermString As String
SearchArray = Array("the", "men", "good")
Set myRange = ActiveDocument.Range
For i = 0 To UBound(SearchArray)
TermString = SearchArray(i)
With myRange.Find
.Text = TermString
.Replacement.Font.Color = wdColorGreen
.Replacement.Text = TermString
.MatchWholeWord = True
.Execute Replace:=wdReplaceAll
End With
Next
End Sub

Works great Greg. Thanks. Can I ask you another question - can you
tell me what the coding is for one macro to call another?

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org


McCain/Palin '08 !!!
 
J

jerem

I encourage the beavering, fellas. I'm the better for it in the end.

Graham, as far as using a different pdf converter -- I use what my company
provides and it has always been taboo in this firm and others I've worked for
to use the OCR doc generated from the pdf for reasons of potential corruption
to the document. It does not take a whole lot to make a document unstable in
Word. Word has got to be the glitchiest WP program I've ever used - whether
the root of the problem is with the design of the program itself, or the fact
that companies tailor the program to suit their every needs, or adding third
party software on top of that (numbering suites, DocX tools, etc.) to enhance
the functionality - and I've used many, from Wang, IBM Displaywriter to
WordPerfect -- Anyway, I'm not using Word right out of the box and it has
always been the rule to strip documents right down to bare text before
applying styles. Nothing worse than a 100+ page ground lease, or even worse,
a 100+ Agreement going corrupt 1 hour before a Closing. Suffice it to say -
this kind of pressure does not produce diamonds.

So, long story short - I shall copy all this code and fool around with it
tomorrow. To carry on with the metaphor - will I get a first down or run it
in for the touchdown? I'll let you know.

P.S.: Where can I find this Abbyy Finereader demo? I'm game to give it a
shot.
 
J

Jentimus

Greg, I've been trying to replace all Italic text with Bold text, and a
slight modification of your macro worked perfectly!

In case another beginner needs this, here's the modification:

Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Text = ""
.Font.Italic = True
With .Replacement
.Text = ""
.Font.Bold = True
.Font.Italic = False
End With
.Execute Replace:=wdReplaceAll
End With
End Sub

You are the pot-o'-gold at the end of a very long search - thanks!

Jen
 
J

jerem

Well Greg,

You have done it again. If I recall correctly, I've proposed to you in the
past and since I've taken your silence as a no, then my only form of
gratitude can be expressed by naming my first born son after you. Ooops -
that's 20 years too late - will you settle for my naming a cherished pet
after you?

You killed two birds with one stone here. Basically speaking, I'm a cheap
date - I don't require elegance, just functionality but as far as I'm
concerned, it is beyond elegant - it's quite exquisite.

Thanks.
 
J

jerem

Well Greg,

If I recall correctly, I’ve proposed to you once before and since I’ve
interpreted your silence on the matter as a no, then the only way I can
express my gratitude to you is to name my first born son after you. Oooops,
that’s twenty years too late. Will you settle for naming a cherished pet
after you?

You managed to kill two birds with one stone here. I have to tell you: I’m
a cheap date. I don’t require elegance, just functionality, but as far as
I’m concerned this is beyond elegant (I used your “A little more elegant
perhaps†version). It’s absolutely exquisite.

Thanks.

jerem
 

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