Macros written to format text between wildcards ends up deleting t

M

MarS

I am new to writing macros. In Word 2003 I have recorded a macro that Finds
& Replaces the font formatting and size when using wildcards (so that any
string of text between ;;*;; is formatted to Tahoma Bold 12 pt; or any text
between @*@ is formatted to Tahoma Regular 11pt, etc.). As I am recording
the macro, the font style and size is replaced correctly, but when I try to
run the macro in other documents (merged from a database) it just deletes the
text.

What am I doing wrong?

Example:

Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "\;;*\;;"
.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
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "\@*\@"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
 
K

Klaus Linke

Hi MarS,

The macro recorder didn't record what you want to do with the found text.

The top of your macro should look something like

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Name = "Tahoma"
Selection.Find.Replacement.Font.Bold = True
Selection.Find.Replacement.Font.Size = 12
With Selection.Find
' ...

If you don't specify any replacement formatting, Word takes
.Replacement.Text = ""
literally, and replaces the found text with nothing.

Regards,
Klaus
 
M

MarS

Thanks, Klaus. I know the macro is not recording any key strokes because I
am selecting formatting changes from pull-down menus, but I'll still give it
a shot. Look out VBA!
 

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