Why does Word hang with this macro?

M

Mark

Hi
I have tried Andrew Savikas' macro as shown at:
http://hacks.oreilly.com/pub/h/2597

I have tried and retried it, but, on my PC (XP and Word XP (latest
patches) ) Word stops responding when I use it. I've had a bit of a look at
what it is
doing, but I cannot see why it would lock up Word.

Any ideas?

All I really need to do is search and replace all occurences of "Main Text
Char *" style with "Main Text" and so forth for a few non standard styles
(once I have one working, I can handle the rest).
Thus, Andrew's macro is a bit more than I need, but if it were to work would
do the job.

Many thanks.
 
M

Mark

Hi

It has occured to me that one might be able to use LEFT to define "Main
Text". I'm not sure how.
Something like
If style left 9 = "Main Text" then replace with style "Main Text"

How would you write that in VBA?

Mark
 
M

Mark

Many thanks

Now I'm no expert, but I tried the followingcombining other ideas. It runs
ok, but is not actually changing Main Text. (I used "Main Text " as I
realised I have Main Text Indent that I would need to change in a seperate
run)

Sub Find_Replace_MainT()
Dim s As Style
For Each Style In ActiveDocument.Styles
If Left$(s.NameLocal, 10) = "Main Text " Then
s.NameLocal = "Main Text"
Exit For
End If
Next
End Sub


Any ideas?

Mark
 
M

Mark

Many thanks for your help.

The following seems to run, but does not change anything. I.e. Main Text *
are not changing to "Main Text." The other problem is that it complains
about "s.NameLocal = "Main Text"" as Main Text already exists. Is there a
way to replace Main Text * with Main Text. What am I doing wrong?

Mark


Sub Find_Replace_MainT()
Dim pIndex As Long
For pIndex = ActiveDocument.Styles.Count To 1 Step -1
Set s = ActiveDocument.Styles(pIndex)
If Left$(s.NameLocal, 10) = "Main Text " Then
s.NameLocal = "Main Text"
Exit For
End If
Next
End Sub
 
J

Jean-Guy Marcil

Mark was telling us:
Mark nous racontait que :
Many thanks for your help.

The following seems to run, but does not change anything. I.e. Main
Text * are not changing to "Main Text." The other problem is that it
complains about "s.NameLocal = "Main Text"" as Main Text already
exists. Is there a way to replace Main Text * with Main Text. What am
I doing wrong?
Mark


Sub Find_Replace_MainT()
Dim pIndex As Long
For pIndex = ActiveDocument.Styles.Count To 1 Step -1
Set s = ActiveDocument.Styles(pIndex)
If Left$(s.NameLocal, 10) = "Main Text " Then
s.NameLocal = "Main Text"
Exit For
End If
Next
End Sub

Try this and see my comments in the beginner group (I think it was the
beginner group...):

'_______________________________________
Sub Find_Replace_MainT()

Dim pIndex As Long
Dim MyStyle As Style
Const TempStyleName As String = "TempStyle"
Const RegularName As String = "Main Text"

'Save any paragraph where the Normal style is applied
With ActiveDocument
.Styles.Add TempStyleName
With Selection.Find
.ClearFormatting
.Style = ActiveDocument.Styles("Normal")
.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
With .Replacement
.ClearFormatting
.Style = ActiveDocument.Styles(TempStyleName)
.Text = ""
End With
.Execute Replace:=wdReplaceAll
End With

'Delete all Char styles derived from Main Text
'Paragraphs where a deleted style was applied will
'revert to the Normal style
For pIndex = .Styles.Count To 1 Step -1
Set MyStyle = .Styles(pIndex)
With MyStyle
If Left$(.NameLocal, 10) = RegularName & " " Then
.Delete
End If
End With
Next

'Apply Main Text to all Normal paragraphs
With Selection.Find
.ClearFormatting
.Style = ActiveDocument.Styles("Normal")
.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
With .Replacement
.ClearFormatting
.Style = ActiveDocument.Styles(RegularName)
.Text = ""
End With
.Execute Replace:=wdReplaceAll
End With

'Delete the TempStyle style and if any pargraph were formatted
'with it, they will be returned to their original "Normal" style
.Styles(TempStyleName).Delete

End With

End Sub
'_______________________________________

Please, try not to ask the same questions in many places, If my code here is
sufficient, why should Jezebel waste time writing an answer that will be
ignored. And vice versa, if Jezebel had replied before I did, you might have
totally ignored my answer in the other group and thus I would have ended up
wasting time for nothing...

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

Mark

Thanks
Mark
Jezebel said:
If the problem is that you have multiple styles whose names begin "Main
Text
", your routine will deal with only the first ... Your 'exit for' bales
out
of the loop as soon as you get a match, ignoring the possibility of
further
matches.

Word also has a problem with For ... next loops if you delete collection
members while iterating the loop. Hence, presumably, the original macro's
trick of iterating backwards:

Dim pIndex as long
for pIndex = ActiveDocument.Styles.Count to 1 step -1
Set s = activedocument.Styles(pIndex)
...
Next
 

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