L
larrysulky
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....
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....