Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / August 2005

Tip: Looking for answers? Try searching our database.

Macro Style Replace

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
JohnTheTemp - 16 Aug 2005 18:14 GMT
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?

Signature

JohnTheTemp

Jay Freedman - 16 Aug 2005 19:57 GMT
> Word2000.
>
[quoted text clipped - 6 lines]
> stylename, it search for a stylename plus border attributes plus
> other things. Can this be fixed?

See http://word.mvps.org/FAQs/MacrosVBA/ModifyRecordedMacro.htm.

Signature

Regards,
Jay Freedman
Microsoft Word MVP          FAQ: http://word.mvps.org

Graham Mayor - 17 Aug 2005 06:17 GMT
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

Signature

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

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> Word2000.
>
[quoted text clipped - 6 lines]
> stylename, it search for a stylename plus border attributes plus
> other things. Can this be fixed?
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.