style using macro

B

Bob Wickham

Hi,
I have recorded a macro that searches a document for Normal style and
changes it to Body Text + Justified Style.
It works perfectly while I'm recording it, but it wont work at all after
that.

For instance, before I started recording I manually changed the text to
Normal style, then I recorded the macro that changed it to Body Text +
Justified.
To test it after recording I manually changed the text to Normal style
again. I then ran the macro, but nothing happens

Sub Macro12()
'
' Macro12 Macro
' Macro recorded 23/03/2007 by Bob
'
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Normal")
Selection.Find.ParagraphFormat.Borders.Shadow = False
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles("Body Text")
With Selection.Find.Replacement.ParagraphFormat
.SpaceBeforeAuto = False
.SpaceAfterAuto = False
.Alignment = wdAlignParagraphJustify
End With
Selection.Find.Replacement.ParagraphFormat.Borders.Shadow = False
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

Am I missing the point here or is there something more sinister at work.

Thanks

Bob
 
S

Stefan Blom

Recording a find and replace is tricky (a) because Word uses the
Selection object, when a Range object would be more appropriate, and
(b) because a lot of unneeded stuff may be included (settings that may
have been relevant when you recorded the macro but not necessarily are
so when you run it again).

Try this:

With ActiveDocument.Content.Find
.ClearFormatting
.Style = wdStyleNormal
With .Replacement
.ClearFormatting
.Style = wdStyleBodyText
.ParagraphFormat.Alignment = wdAlignParagraphJustify
End With
.Execute Replace:=wdReplaceAll
End With

I'm not sure why you want to change the alignment to justified when
you could just modify the Body Text style to include that format,
though.

--
Stefan Blom
Microsoft Word MVP


in message
news:%[email protected]...
 

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