If you copied without the last paragraph, the only styles that will come
across are IN USE (i.e. Applied to text, or referenced by styles that are
applied to text).
For example: If you are using Body Text Indent, which is based on Body
Text, Which is based on Normal, when you copy text formatted with Body Text
Indent, all three styles are copied.
There are a large number of built-in styles, and a greater number of
"Default" styles. Built-in styles are required parts of a Word document:
they cannot be deleted. Default styles are provided in a blank Word
document: they can be deleted.
In VBA, run a "For Each Next" loop to find them... Here's a couple of
macros you can play around with. DISCLAIMER: The attached macros will
completely destroy your document and remove all content from it forever. Or
not. Run them on a COPY
There are multiple kinds of styles: for your purposes, test for
Type=Paragraph. Otherwise you will have to inspect the document
character-for-character and in Mac Word the macro will run for a week!
To find unused styles, you must test for both "Builtin" and "InUse". If the
style is built-in, your macro will crash if you attempt to delete it. If
the style is in use, the text will default to Normal style when you delete
the original style.
If the style reports as "InUse", it's probably lying: all that means is that
"at some time in the past" that style was used in the document. It may
still be, or not. The second macro performs a (very slow...)
paragraph-by-paragraph inspection of the text to see if the style actually
IS still in use.
Basically, I would not bother deleting unused styles: a style takes up
practically no space in a document (about 24 bytes) and getting them out is
a pain
Instead, display the formatting palette and change the
Styles>List drop-down to "Styles in use". That thins the list out a lot.
Word, by the way, has an accurate idea of what is in use and what isn't in
that dialog. Regrettably, it does not return this information to VBA so we
can't use it.
Hope this helps
Sub ListStylesInDocument()
Dim StyleNames() As String
Dim astyle As Style
Dim i As Long
i = ActiveDocument.Styles.Count
' Create an array to hold the actual style names
' Re-size the array to the actual number of paragraphs
ReDim StyleNames(i)
i = 1
For Each astyle In ActiveDocument.Styles
StyleNames(i) = astyle.NameLocal
i = i + 1
Next astyle
' VBA does not have a SortArray command
WordBasic.SortArray StyleNames
' New document to hold the output
Application.Documents.Add
' For each name we have
' List it only if this is the first mention of the name
For i = 1 To UBound(StyleNames)
If StyleNames(i) <> StyleNames(i - 1) Then
Selection.TypeParagraph
Selection.TypeText (StyleNames(i))
End If
Next i
End Sub
Sub ListStylesInUse()
'
' ListStylesInUse Macro
' Macro copyright 09/05/03 by John McGhie
' McGhie Information Engineering Pty Ltd
'
' Word has a major design bug: Style names remain in the
' Styles Collection if they were EVER used in the document
' It is impossible to easily discover whether a particular
' Style is actually still in use. This has been fixed in
' Word 2003. For earlier versions, this crude utility
' examines every paragraph to see what style it has, then
' lists each unique style name it found actually applied to
' a paragraph. Slow and horrible, but it works!
Dim aRange As Range
Dim aPara As Paragraph
Dim i As Integer
Dim n As Integer
Dim StyleNames() As String
Dim astyle As Style
Dim NameFound As Boolean
' Count the actual paragraphs in the document
' (including headers, footers, etc)
For Each aRange In ActiveDocument.StoryRanges
i = i + aRange.Paragraphs.Count
Next aRange
' Create an array to hold the actual style names
' Re-size the array to the actual number of paragraphs
ReDim StyleNames(i)
i = 1
For Each aRange In ActiveDocument.StoryRanges
For Each aPara In aRange.Paragraphs
StyleNames(i) = aPara.Style.NameLocal
i = i + 1
Next aPara
Next aRange
' VBA does not have a SortArray command
WordBasic.SortArray StyleNames
' New document to hold the output
Application.Documents.Add
' For each name we have
' List it only if this is the first mention of the name
For i = 1 To UBound(StyleNames)
If StyleNames(i) <> StyleNames(i - 1) Then
Selection.TypeParagraph
Selection.TypeText (StyleNames(i))
End If
Next i
End Sub
I don't want to remove the built-in styles, just the custom styles
that are cluttering up these documents because the editor doesn't
understand about templates, Normal or otherwise. I'm seeing styles for
elements we haven't used in years. Such the headache! And I don't want
to have to scroll through a huge long list to stylize a document.
Thanks for your help! I'll try out that script tomorrow!
I'm on a Mac Word 2004.
--
Please reply to the newsgroup to maintain the thread. Please do not email
me unless I ask you to.
John McGhie <
[email protected]>
Microsoft MVP, Word and Word for Macintosh. Business Analyst, Consultant
Technical Writer.
Sydney, Australia +61 (0) 4 1209 1410