macro to find style "Main Body Text Char *" and replaced it

M

Mark K

Re macro to find style "Main Body Text Char *" and replaced it with
"Main Body Text"

I have older files that need to be edited and sent off. They have the
old Char Char problem. The style I'm concerned with is a created
style "Main Body Text" and its mutations Main Body Text Char Char,
Char Char Char Char, Char Char Char Char Char Char Char Char, and so
forth.

At present I search for these mutations and replace them manually with
"Main Body Text" - they are not linked so that is easy. Then
delete them manually. Problem is some documents have so many it takes
for ever.

If only I had a macro that would find all occurrences of the style
"Main Body Text Char *" and replaced it with "Main Body Text" I
tried the following macro but it does pick them up.

I have read many of the responses and tried solutions, but so far have
not been able to crack my problem.

Any ideas?
Many thanks,
Mark

Sub Macro1()
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Main Body Text Char
*")
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles("Main Body
Text")
 
J

Jean-Guy Marcil

Mark K was telling us:
Mark K nous racontait que :
Re macro to find style "Main Body Text Char *" and replaced it with
"Main Body Text"

I have older files that need to be edited and sent off. They have the
old Char Char problem. The style I'm concerned with is a created
style "Main Body Text" and its mutations Main Body Text Char Char,
Char Char Char Char, Char Char Char Char Char Char Char Char, and so
forth.

At present I search for these mutations and replace them manually with
"Main Body Text" - they are not linked so that is easy. Then
delete them manually. Problem is some documents have so many it takes
for ever.

If only I had a macro that would find all occurrences of the style
"Main Body Text Char *" and replaced it with "Main Body Text" I
tried the following macro but it does pick them up.

I have read many of the responses and tried solutions, but so far have
not been able to crack my problem.

Is this on Word XP?
Have you tried turning off Keep track of formatting (Tools > Options... >
Edit tab)? This is the culprit for creating this nightmare.

Then, sometimes all it takes is a CTRL-A and CTRL-Q and CTRL-Space Bar
(Select All and Reset Paragraphs and Reset Fonts). You will lose all manual
formatting, but if you delete all the styles I think you will lose
formatting as well anyway.

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

Mark K

Thanks for your help. Yes I have previously tried this, but
unfortunately ^q does not fix the Main Text Char Char etc etc.
Any macro ideas?

Mark
 
J

Jean-Guy Marcil

Mark K was telling us:
Mark K nous racontait que :
Thanks for your help. Yes I have previously tried this, but
unfortunately ^q does not fix the Main Text Char Char etc etc.
Any macro ideas?

You have to write a macro that would iterate through each style (with For
Each... Next for example), get the style name in a string, check if the
string contains " Char" (With the space) and if so, delete the style or
rename it.

All text that was affected to a deleted style will revert to the Normal
style.
If the Normal style is not actually used in the document, then when you are
done, replace all text with the Normal style with the style you actually
want to use.

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

Mark

Hi
I'm not a VBA expert. The following which is combining a few
ideas/suggestions 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 :
Hi
I'm not a VBA expert. The following which is combining a few
ideas/suggestions 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?

The problem with renaming is that at the first occurrence of a style that
the macro finds that must be renamed, it tries to apply a name that already
exists... No can do!

The other alternative would be to find each paragraph formatted with a Main
Text char style, apply the regular Main Text style, then just delete the
char styles.

That might be very slow...

Here is an alternative idea:

'_______________________________________
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 TEmnpStyle style and if any pargraph were formatteed
'with it, they will be returned to their original "Normal" style
.Styles(TempStyleName).Delete

End With

End Sub
'_______________________________________

The drawback is that if lots of text had manually been "formatted" with the
Normal style, all those manual alterations to paragraph format will be lost,
the same to all manual formatting that had might have been applied to any
char style. Font formatting will be preserved.
I do not think this can be avoided when dealing with styles, short of
testing each paragraph, storing their paragraph formatting parameters,
changing the style, then reapplying the manual formatting. Very tedious and
the macro would run very slowly, I think.
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
M

Mark

Many thanks

My that is a lot of work, much appraciated.

It works to the point that Main Text Char etc is converted to Normal. To the
point of the code of:

..Style = ActiveDocument.Styles(RegularName)

And the error "style name exists ..." occurs.

Any clues on how to fix this one?

Thanks
Mark
 
J

Jean-Guy Marcil

Mark was telling us:
Mark nous racontait que :
Many thanks

My that is a lot of work, much appraciated.

It works to the point that Main Text Char etc is converted to Normal.
To the point of the code of:

.Style = ActiveDocument.Styles(RegularName)

And the error "style name exists ..." occurs.

I am not sure I understand. This line of code cannot generate the error you
are seeing because it does not try to manipulate style names, but merely
formatting text with a given style. Of course the style exists, it has
to.... Otherwise we could not format the text with that style.
Any clues on how to fix this one?

Not really. I ran the code several times in a test document without any
problems... Of course, your document is more complex than mine.... but
still, it should work.

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

Hank Roberts (at the office)

I copied the macro and tested it on a fairly complex document, and got a
halt and error message at the line
.Style = ActiveDocument.Styles(RegularName)

Doesn't the macro as written expect that my document already has a defined
style named either "RegularName" or "Main Text" -- should that line instead
say
.Style = ActiveDocument.Styles(Normal)
at that point?

Written in haste, while trying to figure out something else entirely -- I'll
come back to bang on this when I have time, so I'm just confirming I see a
similar problem.

And adding my suspicion that I just haven't read carefully enough to
recognize something that I'm doing wrong here (grin).
 
H

Hank Roberts (at the office)

One other thought -- sometimes I can sneak around problems like this by
finding the target and replacing it with "font color green" -- finish that,
so you've done something unique to all your targets -- then go and run
through again and change whatever's green to your desired end result.

I have no idea why this works sometimes, when I can't get a direct
replacement to work -- but it does, consistently, pay off when nothing else
is working.

Of course, "your color may vary" -- or whatever other aspect you choose to
use as the interim state. Just has to be something Word is willing to do for
you.

Mark said:
Many thanks

My that is a lot of work, much appraciated.

It works to the point that Main Text Char etc is converted to Normal. To the
point of the code of:

..Style = ActiveDocument.Styles(RegularName)

And the error "style name exists ..." occurs.

Any clues on how to fix this one?
.....
 
M

Mark

Thanks
Mark
"Hank Roberts (at the office)"
One other thought -- sometimes I can sneak around problems like this by
finding the target and replacing it with "font color green" -- finish
that,
so you've done something unique to all your targets -- then go and run
through again and change whatever's green to your desired end result.

I have no idea why this works sometimes, when I can't get a direct
replacement to work -- but it does, consistently, pay off when nothing
else
is working.

Of course, "your color may vary" -- or whatever other aspect you choose to
use as the interim state. Just has to be something Word is willing to do
for
you.


....
 

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