L
Larry
Hi, All -- Some time ago I started a thread about deleting "xxx Char"
styles. One of the solutions offered did a good job but reverted any
paragraph that had a "xxx Char" style in it to the Normal paragraph
style, which was rather heavy-handed. Here's an updated version that
re-applies the intended paragraph style:
Sub DeleteAutoCharStyles()
Dim myStyle As Style
Dim myStyleLinkStyle As Style
Dim myLog As String
myLog = ""
For Each myStyle In ActiveDocument.Styles
If myStyle.Type = wdStyleTypeCharacter And _
myStyle.LinkStyle <> ActiveDocument.Styles
(wdStyleNormal) Then
Set myStyleLinkStyle = myStyle.LinkStyle
Debug.Print myStyleLinkStyle, Asc(myStyle.NameLocal),
myStyle
myStyle.LinkStyle = ActiveDocument.Styles(wdStyleNormal)
myStyleLinkStyle.LinkStyle = ActiveDocument.Styles
(wdStyleNormal)
End If
If (myStyle.BuiltIn = False) And (Right(myStyle.NameLocal, 5)
= " Char") Then
Dim realStyleName As String
realStyleName = left(myStyle.NameLocal, Len
(myStyle.NameLocal) - 5)
'MsgBox realStyleName, , "DEBUG: realStyleName"
Selection.HomeKey Unit:=wdStory
With Selection.Find
.ClearFormatting
.Style = ActiveDocument.Styles(myStyle.NameLocal)
.Replacement.ClearFormatting
.Replacement.Style = ActiveDocument.Styles
(realStyleName)
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Debug.Print "", "Deleting " & myStyle.NameLocal, Asc
(myStyle.NameLocal)
myLog = myLog & myStyle.NameLocal & vbCrLf
myStyle.Delete
deleteCount = deleteCount + 1
End If
Next
Bye:
If (myLog <> "") Then
MsgBox myLog, , "Deleted these bogus character styles:"
Else
MsgBox "No bogus character styles found.", , "Done!"
End If
End Sub
A single pass through will not catch multiply-suffixed styles ("H1
Char Char Char"), but just running the macro until it reports all
clear should do the trick. BTW, I never did find a way to
programmatically delete styles whose name begins with a space
character (" Char Char", for example), but have been using the style
organizer to get rid of them.
Hope this is of use.
--L
styles. One of the solutions offered did a good job but reverted any
paragraph that had a "xxx Char" style in it to the Normal paragraph
style, which was rather heavy-handed. Here's an updated version that
re-applies the intended paragraph style:
Sub DeleteAutoCharStyles()
Dim myStyle As Style
Dim myStyleLinkStyle As Style
Dim myLog As String
myLog = ""
For Each myStyle In ActiveDocument.Styles
If myStyle.Type = wdStyleTypeCharacter And _
myStyle.LinkStyle <> ActiveDocument.Styles
(wdStyleNormal) Then
Set myStyleLinkStyle = myStyle.LinkStyle
Debug.Print myStyleLinkStyle, Asc(myStyle.NameLocal),
myStyle
myStyle.LinkStyle = ActiveDocument.Styles(wdStyleNormal)
myStyleLinkStyle.LinkStyle = ActiveDocument.Styles
(wdStyleNormal)
End If
If (myStyle.BuiltIn = False) And (Right(myStyle.NameLocal, 5)
= " Char") Then
Dim realStyleName As String
realStyleName = left(myStyle.NameLocal, Len
(myStyle.NameLocal) - 5)
'MsgBox realStyleName, , "DEBUG: realStyleName"
Selection.HomeKey Unit:=wdStory
With Selection.Find
.ClearFormatting
.Style = ActiveDocument.Styles(myStyle.NameLocal)
.Replacement.ClearFormatting
.Replacement.Style = ActiveDocument.Styles
(realStyleName)
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Debug.Print "", "Deleting " & myStyle.NameLocal, Asc
(myStyle.NameLocal)
myLog = myLog & myStyle.NameLocal & vbCrLf
myStyle.Delete
deleteCount = deleteCount + 1
End If
Next
Bye:
If (myLog <> "") Then
MsgBox myLog, , "Deleted these bogus character styles:"
Else
MsgBox "No bogus character styles found.", , "Done!"
End If
End Sub
A single pass through will not catch multiply-suffixed styles ("H1
Char Char Char"), but just running the macro until it reports all
clear should do the trick. BTW, I never did find a way to
programmatically delete styles whose name begins with a space
character (" Char Char", for example), but have been using the style
organizer to get rid of them.
Hope this is of use.
--L