L
larrysulky
Hi, All--
There's a mess of good info here, and on websites, and particularly on
MVPS, about deleting unused styles. I put together a macro using the
various ideas but I can't seem to get it running quite right (WinXP,
Word2003):
1) Re: Deleting unused but unimportant built-in styles: Forget it. The
macro won't do it. For that matter, the user interface won't do it
either.
2) Re: "styles in use" category under Styles and Formatting => Custom
=> Styles: Useless. A pack of lies.
3) Re: Deleting styles in floating textboxes: this is the kicker. These
documents have lots of textboxes and lots of styles. I try to use a
simple Find command to find content with each style listed in the
active document's style list, but Find within VBA seems unable to
consistently find content in boxes even though when Find is run from
the interface it has no problem. The result is that lots of styles are
flagged as "unused" even though I can look right at them being used.
Here's the macro (with big blocks of comments excised to save space).
Anybody have any suggestions? TIA...
~~~~~~~~~~~~~~~~~~~~~~~~~
Sub DeleteUnusedStyles() ' Much of this was nicked from an MVP site.
Dim myStyle As Style
Dim myDeletePrompt As String
Dim rngStory As Word.Range
Dim lngJunk As Long
Dim myFlagFoundStyle As Boolean ' True when style truly appears to be
in use.
Selection.HomeKey Unit:=wdStory ' Is this necessary?
For Each myStyle In ActiveDocument.Styles
myFlagFoundStyle = False
If (myStyle.BuiltIn = True) Then
'' Protect all built-in styles until I get everything else
working.
myFlagFoundStyle = True
End If
If (myFlagFoundStyle = False) Then
lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
' ... Something about a header bug?
'Selection.HomeKey Unit:=wdStory ' Is this necessary?
'Iterate through all story types in the current document
For Each rngStory In ActiveDocument.StoryRanges
rngStory.Find.ClearFormatting
rngStory.Find.Replacement.ClearFormatting
With rngStory.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Style = ActiveDocument.Styles(myStyle.NameLocal)
End With
'Iterate through all linked stories
Do
If (rngStory.Find.Execute) Then
myFlagFoundStyle = True
End If
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next
End If
If (Not myFlagFoundStyle) Then
myDeletePrompt = "Delete unused user style?"
Select Case MsgBox(myDeletePrompt, vbYesNoCancel + vbInformation,
myStyle.NameLocal)
Case vbYes
myStyle.Delete
Case vbCancel
GoTo bye
End Select
End If
Next myStyle
bye:
End Sub
There's a mess of good info here, and on websites, and particularly on
MVPS, about deleting unused styles. I put together a macro using the
various ideas but I can't seem to get it running quite right (WinXP,
Word2003):
1) Re: Deleting unused but unimportant built-in styles: Forget it. The
macro won't do it. For that matter, the user interface won't do it
either.
2) Re: "styles in use" category under Styles and Formatting => Custom
=> Styles: Useless. A pack of lies.
3) Re: Deleting styles in floating textboxes: this is the kicker. These
documents have lots of textboxes and lots of styles. I try to use a
simple Find command to find content with each style listed in the
active document's style list, but Find within VBA seems unable to
consistently find content in boxes even though when Find is run from
the interface it has no problem. The result is that lots of styles are
flagged as "unused" even though I can look right at them being used.
Here's the macro (with big blocks of comments excised to save space).
Anybody have any suggestions? TIA...
~~~~~~~~~~~~~~~~~~~~~~~~~
Sub DeleteUnusedStyles() ' Much of this was nicked from an MVP site.
Dim myStyle As Style
Dim myDeletePrompt As String
Dim rngStory As Word.Range
Dim lngJunk As Long
Dim myFlagFoundStyle As Boolean ' True when style truly appears to be
in use.
Selection.HomeKey Unit:=wdStory ' Is this necessary?
For Each myStyle In ActiveDocument.Styles
myFlagFoundStyle = False
If (myStyle.BuiltIn = True) Then
'' Protect all built-in styles until I get everything else
working.
myFlagFoundStyle = True
End If
If (myFlagFoundStyle = False) Then
lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
' ... Something about a header bug?
'Selection.HomeKey Unit:=wdStory ' Is this necessary?
'Iterate through all story types in the current document
For Each rngStory In ActiveDocument.StoryRanges
rngStory.Find.ClearFormatting
rngStory.Find.Replacement.ClearFormatting
With rngStory.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Style = ActiveDocument.Styles(myStyle.NameLocal)
End With
'Iterate through all linked stories
Do
If (rngStory.Find.Execute) Then
myFlagFoundStyle = True
End If
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next
End If
If (Not myFlagFoundStyle) Then
myDeletePrompt = "Delete unused user style?"
Select Case MsgBox(myDeletePrompt, vbYesNoCancel + vbInformation,
myStyle.NameLocal)
Case vbYes
myStyle.Delete
Case vbCancel
GoTo bye
End Select
End If
Next myStyle
bye:
End Sub