Macro Style Replace

J

JohnTheTemp

Word2000.

I record a macro to SearchAndReplace a custom style.

During the recording, the SearchAndReplace works fine.

When running the macro, it doesn't work. Calling up the SearchAndReplace
dialog box shows why: instead of searching for the stylename, it search for a
stylename plus border attributes plus other things. Can this be fixed?
 
G

Graham Mayor

This sort of information is beyond the capabilities of the macro recorder -
the following should do what you want:
http://www.gmayor.com/installing_macro.htm

Sub ReplaceStyle()
Dim FindStyle As String
Dim ReplaceStyle As String

FindInput:
FindStyle = InputBox("Enter name of STYLE to change", _
"Find Style")

ReplaceInput:
ReplaceStyle = InputBox("Replace " & _
FindStyle & " with which STYLE?", _
"Replacement Style")

Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
On Error GoTo FindError
Selection.Find.Style = ActiveDocument.Styles(FindStyle)
Selection.Find.Replacement.ClearFormatting
On Error GoTo ReplaceError
Selection.Find.Replacement.Style = _
ActiveDocument.Styles(ReplaceStyle)
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

Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles(ReplaceStyle)
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = _
ActiveDocument.Styles("Default Paragraph Font")
With Selection.Find
.Text = ""
.Replacement.Text = ""
End With
Selection.Find.Execute replace:=wdReplaceAll

End
FindError:
MsgBox Prompt:=FindStyle & " Style does not exist", _
Buttons:=vbExclamation, _
Title:="Error!"
GoTo FindInput
ReplaceError:
MsgBox Prompt:=ReplaceStyle & " Style does not exist", _
Buttons:=vbExclamation, _
Title:="Error!"
GoTo ReplaceInput

End Sub


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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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