Word 2003 find and replace style macro not working

D

Daem0nX

I've recorded a macro "fnr" (find n replace) that finds all styles
called H3Par and changes them over to macroH3. When I record the macro
it finds and replaces all instances of the style. However, if I reload
my document and run fnr, it doesn't find/replace any styles. The only
changes I've made to the recorded macro are If StyleExists and MsgBox -
to make sure 1) the style exists and it doesn't error out 2) to confirm
it ran the macro. I've ran the macro without the changes (default from
recording) also.

Sub fnr()
'
' fnr Macro
' Macro recorded 8/22/2006 by byron
'
If StyleExists("H3Par") Then
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("H3Par")
Selection.Find.ParagraphFormat.Borders.Shadow = False
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles("macroH3")
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
MsgBox "Macro ran fine"
End If
End Sub

If you need any other details let me know.
Any help is appreciated =)
 
J

Jean-Guy Marcil

Daem0nX was telling us:
Daem0nX nous racontait que :
I've recorded a macro "fnr" (find n replace) that finds all styles
called H3Par and changes them over to macroH3. When I record the macro
it finds and replaces all instances of the style. However, if I reload
my document and run fnr, it doesn't find/replace any styles. The only
changes I've made to the recorded macro are If StyleExists and MsgBox
- to make sure 1) the style exists and it doesn't error out 2) to
confirm it ran the macro. I've ran the macro without the changes
(default from recording) also.

Sub fnr()
'
' fnr Macro
' Macro recorded 8/22/2006 by byron
'
If StyleExists("H3Par") Then
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("H3Par")
Selection.Find.ParagraphFormat.Borders.Shadow = False
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles("macroH3")
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
MsgBox "Macro ran fine"
End If
End Sub

If you need any other details let me know.
Any help is appreciated =)

This is because "StyleExists" is not a method/property from the Word object
library.
I was thinking that if you want to check if H3Par exists, shouldn't you also
check to see if "macroH3" also exists?
If that is not important, remove the second Next/If in the sub I suggest
below.

You should also remove the ParagraphFormat border stuff that the recorder
always seems to add, even though you probably didn't specify it.
For more on modifying macros recorded with the recorder see:
http://word.mvps.org/faqs/macrosvba/ModifyRecordedMacro.htm

Now try this instead:

'_______________________________________
Sub fnr()

Dim docStyles As Styles
Dim i As Long
Dim j As Long

Set docStyles = ActiveDocument.Styles

For i = 1 To docStyles.Count
If docStyles(i).NameLocal = "H3Par" Then
For j = 1 To docStyles.Count
If docStyles(j).NameLocal = "macroH3" Then
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("H3Par")
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style =
ActiveDocument.Styles("macroH3")
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
MsgBox "Macro ran fine"
Exit Sub
End If
Next
End If
Next

MsgBox "The style ""H3Par"" or the style ""macroH3"" do not exist " _
& "in this document."

End Sub
'_______________________________________


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
B

ByronR

I probably should have included my StyleExists function =)

Private Function StyleExists(styleName As String) As Boolean
Dim currentStyle As Style
Dim stylePresent As Boolean
stylePresent = False

' Check for existence of style in active document.
For Each currentStyle In ActiveDocument.Styles
If currentStyle.NameLocal = styleName Then
stylePresent = True
Exit For
End If
Next currentStyle

' Return.
StyleExists = stylePresent
End Function

I found an example late last night that does seem to be working,
however I'll most likely try yours in the near future to compare the
two.

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Style = "H3Par"
.Replacement.Text = ""
.Replacement.Style = "macroH3"
End With
Selection.Find.Execute replace:=wdReplaceAll

Now out of curiosity, since I have a list of roughly 60 styles I need
to modify. How difficult is it, or would it be to create an array to
loop through and have 1 code block doing the updating instead of 60
individual code blocks?
 
J

Jean-Guy Marcil

ByronR was telling us:
ByronR nous racontait que :
I probably should have included my StyleExists function =)

Private Function StyleExists(styleName As String) As Boolean
Dim currentStyle As Style
Dim stylePresent As Boolean
stylePresent = False

' Check for existence of style in active document.
For Each currentStyle In ActiveDocument.Styles
If currentStyle.NameLocal = styleName Then
stylePresent = True
Exit For
End If
Next currentStyle

' Return.
StyleExists = stylePresent
End Function

I found an example late last night that does seem to be working,
however I'll most likely try yours in the near future to compare the
two.

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Style = "H3Par"
.Replacement.Text = ""
.Replacement.Style = "macroH3"
End With
Selection.Find.Execute replace:=wdReplaceAll

Now out of curiosity, since I have a list of roughly 60 styles I need
to modify. How difficult is it, or would it be to create an array to
loop through and have 1 code block doing the updating instead of 60
individual code blocks?

Not really difficult.

I would put the find/replace code in a function and call it from a loop that
would iterate the style names from the array.
Roughly, for example:

For i = 1 to UBound(myArray, 2)
myReplace myArray(0,i), myArray(1,i)
Next

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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