I need to programmatically delete styles whose names begin with a
space character. I know that it's not possible for a user to create
such a thing intentionally -- the style modification/creation dialogue
suppresses such leading spaces. But the paragraph/character hybrid
style logic that Word 2003 and later uses can lead users to
accidentally create styles with names of " Char Char", " Char Char
Char", and so on.

If you record a macro, delete a " Char Char" style by hand, and then
look at what you have recorded, you'll see:

ActiveDocument.Styles(" Char Char").Delete

But simply running this as a VBA command does not actually work: "The
requested member of the collection does not exist".

It also does not work to declare a style variable and use For Each
logic to try to catch it:

Dim myStyle As Style
For Each myStyle In ActiveDocument.Styles
If (myStyle.NameLocal = " Char Char") Then
Debug.Print "", "_Deleting " & myStyle.NameLocal
myStyle.Delete
End If
Next

(Interestingly, myStyle.NameLocal does resolve correctly in the
debug.print statement.)

I've tried escaping the space in the style name, and using a character
reference instead of a literal space, but I can't find the magic
bullet. Any ideas out there?

TIA....

Re: Deleting styles with leading spaces in their names by Klaus

Klaus
Fri Oct 12 13:03:30 PDT 2007

Hi Larry,

I don't think there is a way, short of re-building the doc (i.e., copying
everything into a clean doc, leaving the buggy style definitions behind).

Wouldn't mind to be proven wrong, though...

Regards,
Klaus


<larrysulky@gmail.com> wrote:
>I need to programmatically delete styles whose names begin with a
> space character. I know that it's not possible for a user to create
> such a thing intentionally -- the style modification/creation dialogue
> suppresses such leading spaces. But the paragraph/character hybrid
> style logic that Word 2003 and later uses can lead users to
> accidentally create styles with names of " Char Char", " Char Char
> Char", and so on.
>
> If you record a macro, delete a " Char Char" style by hand, and then
> look at what you have recorded, you'll see:
>
> ActiveDocument.Styles(" Char Char").Delete
>
> But simply running this as a VBA command does not actually work: "The
> requested member of the collection does not exist".
>
> It also does not work to declare a style variable and use For Each
> logic to try to catch it:
>
> Dim myStyle As Style
> For Each myStyle In ActiveDocument.Styles
> If (myStyle.NameLocal = " Char Char") Then
> Debug.Print "", "_Deleting " & myStyle.NameLocal
> myStyle.Delete
> End If
> Next
>
> (Interestingly, myStyle.NameLocal does resolve correctly in the
> debug.print statement.)
>
> I've tried escaping the space in the style name, and using a character
> reference instead of a literal space, but I can't find the magic
> bullet. Any ideas out there?
>
> TIA....
>



Re: Deleting styles with leading spaces in their names by lf

lf
Fri Oct 12 15:57:00 PDT 2007

It is possible to get rid of the Char styles if the job is carried out in the
correct order. For a macro that may help you getting started, see:
http://homepage.swissonline.ch/cindymeister/MyFavTip.htm#CharStyl

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word


"Klaus Linke" wrote:

> Hi Larry,
>
> I don't think there is a way, short of re-building the doc (i.e., copying
> everything into a clean doc, leaving the buggy style definitions behind).
>
> Wouldn't mind to be proven wrong, though...
>
> Regards,
> Klaus
>
>
> <larrysulky@gmail.com> wrote:
> >I need to programmatically delete styles whose names begin with a
> > space character. I know that it's not possible for a user to create
> > such a thing intentionally -- the style modification/creation dialogue
> > suppresses such leading spaces. But the paragraph/character hybrid
> > style logic that Word 2003 and later uses can lead users to
> > accidentally create styles with names of " Char Char", " Char Char
> > Char", and so on.
> >
> > If you record a macro, delete a " Char Char" style by hand, and then
> > look at what you have recorded, you'll see:
> >
> > ActiveDocument.Styles(" Char Char").Delete
> >
> > But simply running this as a VBA command does not actually work: "The
> > requested member of the collection does not exist".
> >
> > It also does not work to declare a style variable and use For Each
> > logic to try to catch it:
> >
> > Dim myStyle As Style
> > For Each myStyle In ActiveDocument.Styles
> > If (myStyle.NameLocal = " Char Char") Then
> > Debug.Print "", "_Deleting " & myStyle.NameLocal
> > myStyle.Delete
> > End If
> > Next
> >
> > (Interestingly, myStyle.NameLocal does resolve correctly in the
> > debug.print statement.)
> >
> > I've tried escaping the space in the style name, and using a character
> > reference instead of a literal space, but I can't find the magic
> > bullet. Any ideas out there?
> >
> > TIA....
> >
>
>
>

Re: Deleting styles with leading spaces in their names by Klaus

Klaus
Fri Oct 12 16:25:44 PDT 2007

Hi Lene,

Yes, but not if the name starts with a blank, as in Larry's case.

Regards,
Klaus


"Lene Fredborg" <lf@REMOVETHISthedoctools.com> schrieb im Newsbeitrag
news:6499CEBB-EFD9-45A7-9CEB-E167C12E66DB@microsoft.com...
> It is possible to get rid of the Char styles if the job is carried out in
> the
> correct order. For a macro that may help you getting started, see:
> http://homepage.swissonline.ch