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 / March 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mark K - 21 Jan 2005 05:54 GMT
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")
Jean-Guy Marcil - 21 Jan 2005 15:15 GMT
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"
[quoted text clipped - 16 lines]
> 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.

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Mark K - 22 Jan 2005 05:51 GMT
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
Jean-Guy Marcil - 24 Jan 2005 15:53 GMT
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.

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Mark - 25 Jan 2005 06:55 GMT
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

> Mark K was telling us:
> Mark K nous racontait que :
[quoted text clipped - 13 lines]
> are done, replace all text with the Normal style with the style you
> actually want to use.
Jean-Guy Marcil - 25 Jan 2005 16:26 GMT
Mark was telling us:
Mark nous racontait que :

> Hi
> I'm not a VBA expert. The following which is combining a few
[quoted text clipped - 5 lines]
> 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.
Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Mark - 27 Jan 2005 05:32 GMT
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

> Mark was telling us:
> Mark nous racontait que :
[quoted text clipped - 92 lines]
> changing the style, then reapplying the manual formatting. Very tedious
> and the macro would run very slowly, I think.
Jean-Guy Marcil - 27 Jan 2005 18:28 GMT
Mark was telling us:
Mark nous racontait que :

> Many thanks
>
[quoted text clipped - 6 lines]
>
> 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.

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Hank Roberts (at the office) - 07 Mar 2005 20:29 GMT
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).

> Many thanks
>
[quoted text clipped - 11 lines]
> Thanks
> Mark

> > Here is an alternative idea:
> >
[quoted text clipped - 69 lines]
> > changing the style, then reapplying the manual formatting. Very tedious
> > and the macro would run very slowly, I think.
Hank Roberts (at the office) - 07 Mar 2005 20:39 GMT
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.

> Many thanks
>
[quoted text clipped - 8 lines]
>
> Any clues on how to fix this one?
....
Mark - 25 Mar 2005 08:12 GMT
Thanks
Mark
> One other thought -- sometimes I can sneak around problems like this by
> finding the target and replacing it with "font color green"  -- finish
[quoted text clipped - 26 lines]
>> Any clues on how to fix this one?
> ....
 
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.