Word 2004 delete multiple unwanted styles

F

fogharty

I have documents the I am editing that have, no kidding, well over a
hundred styles, although only a fraction of them are in use. I have
created a template with only the styles I need and have tried copying
and pasting (save for the last paragraph mark) into a new document
from that template. However, *all* the styles still come over. I need
to retain some of the styles, paragraph and character styles both, so
pasting as unstyled text wouldn't be an option.

I could use Organizer to delete unwanted styles, but that would be
incredibly time consuming. I can't think of how to record a macro to
do this, and writing a VBA script from scratch is beyond my
capabilities.

Are there any suggestions on how to automaticall ydelete unused/
unwanted styles?
 
L

little_creature

Hello,
Normal template stores a lot of styles which cannot be removed (or at
least easily or without harm) I wouldn't do it.
You can switch to see styles in use/available style/all styles.

If you copy between documents the styles are copied correspondingly.
I have found this VBA script to remove unsued styles, kindly see here:
http://wordtips.vitalnews.com/Pages/T0969_Removing_Unused_Styles.html
You haven't mention which version of office you use. Currently I have
only PC acces, I have tried it on PC 2003 and worked fine. I can test
it on MAc tommorow.
But bear in mind this will not remove the build in styles.
Hope this will help
 
F

fogharty

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.
 
J

John McGhie [MVP - Word and Word Macintosh]

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
 
L

little_creature

Tested this macro:

Sub DeleteUnusedStyles()
Dim oStyle As Style

For Each oStyle In ActiveDocument.Styles
'Only check out non-built-in styles
If oStyle.BuiltIn = False Then
With ActiveDocument.Content.Find
.ClearFormatting
.Style = oStyle.NameLocal
.Execute FindText:="", Format:=True
If .Found = False Then oStyle.Delete
End With
End If
Next oStyle
End Sub

From link bellow on office 2004 works fine for me. I have placed the macro
to *this document*.
 
C

Clive Huggan

Dear "fogharty",

I receive text with contorted styles all the time, quite apart from text
from Web pages. I don't let the styles get into my document in the first
place: I use a keyboard shortcut to paste it in (Paste Special) as
"unformatted" text (usually into a paragraph styled with my definition of
body text, which is not based on Normal -- but that's a separate story). The
text takes on the characteristics of the paragraph into which it's pasted,
so it isn't actually unformatted at all. Then I apply heading and other
styles throughout the document. I have timed this many times, and
invariably find it takes me less time than trying to fiddle with the styling
I receive.

If there is significant italicizing etc, I apply those character styles by
looking at the original on screen or, if a shorter document, sometimes a
hard copy. (I apply that as character styles, not direct formatting.)

More detail is under the headings:

* "Removing styles from copied text to be pasted in" (page 120)
* "Document from someone else ‹ checking and fixing formatting of" (pages
134 and 135)

in some notes on the way I use Word for the Mac, titled "Bend Word to Your
Will", which are available as a free download from the Word MVPs' website
(http://word.mvps.org/Mac/Bend/BendWordToYourWill.html).

[Note: "Bend Word to your will" is designed to be used electronically and
most subjects are self-contained dictionary-style entries. If you decide to
read more widely than the item I've referred to, it's important to read the
front end of the document -- especially pages 3 and 5 -- so you can select
some Word settings that will allow you to use the document effectively.]

Cheers,

Clive Huggan
Canberra, Australia
(My time zone is 5-11 hours different from the US and Europe, so my
follow-on responses to those regions can be delayed)
============================================================
 
F

fogharty

Thanks little creature, this macro worked!
I know that styles don't take up much room, but when you have so many
of them, it can get confusing.
 
F

fogharty

I use a keyboard shortcut to paste it in (Paste Special) as "unformatted" text

Hi Clive, I use this method a lot also; the trouble was in this
instance I need to preserve some of the author's formatting, while add
some of my own. So this wouldn't have worked in this instance.

And as I was creating a custom toolbar for the new styles I would be
using, I noticed that the documents had styles we hadn't used in a
long time, and that there was about 100+ of them. And, as John pointed
out, the styles were coming over even if I didn't copy over the final
paragraph mark.

But this macro gives me a "cleaner slate" from which to work.

Thanks for all the responses! This is probably the best group on the
web!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top