Today I went down the rabbit hole of how to delete hundreds of unused styles in a document. Deleting them manually, even using the Organizer (see my blog post on this: https://cybertext.wordpress.com/2010/01/28/word-remove-unwanted-styles-quickly/), is tedious, so I wanted to find if there was an easier way using a macro or similar.
Bottom line: It may be easier to use a macro or one of the other methods (1 to 4) described below if you have a fairly short and not terribly complex document. BUT if you have a large complex document (e.g. with control boxes for document properties, outline numbering, modified settings for inbuilt styles such as heading and TOC styles), you could be making more work for yourself than doing it manually, consider using the macro in Method 5 (added after this post was originally written).
Test environment: I used a copy of a client’s 440p document, which had appendices, outline numbering, portrait/landscape sections, headers and footers, control boxes for doc properties that were repeated in the headers/footers, automated cross-references, automated caption numbering etc. It also had hundreds of styles, many of which seemed to be unused, that seemed to have come in from other documents or had been created for a specific need—it was these I was trying to get rid of quickly. I printed the list of styles to PDF—this document was 129p long!
My Google searching led me to a couple of sites and forums that seemed to address this issue, a couple of which used macros to achieve the task. One used a version of ‘maggying’ a document (named for the late Maggie Secara, after whom the technique is named), so I tried that first—why use brute force if something simple can achieve what you want?
Method 1: Maggie the document and put it into a new blank document
I found this method here: https://darrengoossens.wordpress.com/2019/04/11/removing-unused-styles-from-a-word-document/. It seemed an ideal solution—quick and simple, with the outcome I wanted. However, it just didn’t work for me. After 45 minutes waiting for the copied text in the 440p document to paste, with ‘not responding’ in the title bar of Word, I finally killed the process. However, I haven’t written off this method as it could be a good solution for smaller documents with many styles. (See Method 4 for some issues I found using this method on a complex document.)
Method 2: Allen Wyatt’s macro
The next thing I tried was a macro from Allen Wyatt, the WordTips guy: https://word.tips.net/T001337_Removing_Unused_Styles.html. From another forum I was aware that the header/footer styles might get removed using his macro, so I was ready to check that. However, I got ‘not responding’ in Word again, and after 40 mins waiting for something to happen, I killed the process again. This may be a solution for a smaller document, but didn’t work for me—I expect a macro to take a few minutes at most.
Method 3: Paul Edstein’s macro
The next macro was one from Paul Edstein (a variation of Allen Wyatt’s DeleteUnusedStyles macro) that I found here: https://www.msofficeforums.com/word-vba/37489-macro-deletes-unused-styles.html. Supposedly, this one didn’t remove header and footer styles, which are always listed as unused even when they are (yes, that’s Word being Word…). This one took about 25 mins to run on my 440p document, and the list of styles seemed to reduce. However, when I checked them, many of the unused styles were still listed and hadn’t been deleted at all. These styles were user-added (not inbuilt styles) and as far as I know, were not linked to other styles, though it’s difficult to tell what’s linked to what.
Method 4: Method 3 + Method 1
As a final test, I took the copy of the document I’d saved from Method 3 and ran Method 1 on it; that is, I ran Paul Edstein’s macro, then used the Maggie method to put the document into a new blank document.
This time the copied text (all 440p) pasted quickly (within a minute) and Word didn’t hang. However, all the unused styles still remaining after running the Method 3 macro were copied across, even though all indications are that they AREN’T used. But the resulting document had further issues that would have to be fixed manually. For example:
- I lost the content in the doc control boxes for the doc #, doc title etc. in the headers/footers and main body of the document—the control boxes were still there, but the content was gone. These were easy enough to add back in by copy/pasting from the original document, but be aware that this content, if you use it, may disappear.
- All style formatting for built-in styles such as Caption, TOC 1, TOC 2 etc. and Table of Figure styles was lost. I could easily import them back in using the Organizer. Heading 1, 2, 3 etc. had lost the outline numbering and instead had reverted to a different numbering schema, different fonts, different colours, etc. Again, I used the Organizer to reestablish these.
With these results, I didn’t bother checking any further.
Was this combined method worth it? No, because of the time taken in identifying and then fixing up the issues with the styles.
******
As a final note, when I printed the styles used in the doc (to PDF, NOT paper), the untouched original had 129p of style definitions listed. I fully expected this to be less after deleting all the unused styles using the methods listed here, but instead the PDF of the styles in the modified document (Method 4) ran to 143p!!! This PDF included the styles I’d manually deleted after checking there were unused. This matches the information I discovered as I was researching this, and that’s that the ‘in use’ styles are ANY styles ever used in the document, whether they are still in use or have been deleted.
There has to be an easier way that actually deletes all the unused styles and that doesn’t create even more work or take time that could be used manually deleting them. And Microsoft needs to look at why ‘in use’ styles include ANY styles EVER used in the document, even after they’ve been deleted—‘in use’ to me means ‘being used right now’, not was used 5 years ago on an older template.
Update 25 Feb 2022: One of the commentators on this post shared another macro, which, after some tweaking (because WordPress changes things like plain quote to smart quotes, hyphens/dashes, underscores and totally removes angle brackets, etc.), I got to work on my long document. I’ve added it as Method 5.
Method 5: Simon Jones’ macro
This macro has two parts—first it identifies how many styles are in-built and how many are custom (user-added). Once it’s done that, you are asked to click Yes or No to continue to delete the unused custom styles. In my test document, this part of the macro identified almost 900 (!!!!) custom styles.
After the macro had run (and it took about 30 minutes to run on the 440p document—an acceptable time, in my opinion), I was left with 38 custom styles that were used in the document. A much more manageable list!
Notes about this macro:
- If you’re using a version of Word prior to Word 2007, you will have to comment out the lines that have ### in them.
- Copy/paste this macro—some of it goes off the visible window
Sub DeleteUnusedStyles() 'Simon Jones, MillStream Designs Ltd, 05/03/2007 'macro originally called DeleteUnusedStylesTake3 Dim objStyle As Word.Style Dim intBuiltInStyles As Integer Dim intStyle As Integer 'Count the number of built-in styles in this document For Each objStyle In ActiveDocument.Styles If objStyle.BuiltIn Then intBuiltInStyles = intBuiltInStyles + 1 End If Next objStyle 'Show summary and ask permission to continue If MsgBox("There are " & intBuiltInStyles & " built-in styles " & vbCrLf & _ "and " & ActiveDocument.Styles.Count - intBuiltInStyles & _ " custom style(s) in this document." & vbCrLf & vbCrLf & _ "Delete unused custom styles?", vbOKCancel + vbExclamation, _ "Delete Unused Styles") = vbOK Then 'OK to continue 'Run through all the styles in the document BACKWARDS - 'so we only delete styles from the end of the collection For intStyle = ActiveDocument.Styles.Count To 1 Step -1 If Not ActiveDocument.Styles(intStyle).BuiltIn Then 'This is a custom style 'See if it is in use With ActiveDocument.Content.Find .ClearFormatting .Style = intStyle .Execute Format:=True If Not .Found Then 'Style is not in use 'We can delete it With ActiveDocument.Styles(intStyle) 'If this style is linked to another we have to unlink them - 'otherwise both styles will be deleted which causes problems - 'this style may be linked to a built-in style which will cause - 'an error if we try to delete it. If .Type <> wdStyleTypeCharacter Then 'Character styles can't be linked from, only linked to 'The Linked property only exists in Word 2007 and above - 'for previous versions, comment out the "If" and "End If" lines below If .Linked Then '### Word 2007+ only 'The LinkStyle property only exists in Word 2002 and above - 'for previous versions, comment out the lines below If Not .LinkStyle Is ActiveDocument.Styles(wdStyleNormal) Then '### Word 2002+ only .LinkStyle = wdStyleNormal '### Word 2002+ only End If '### Word 2002+ only 'LinkStyle is a very badly behaved property and this is the - 'closest we can get to unlinking styles short of copying all - 'text except the last paragraph mark to a new document. End If '### Word 2007+ only End If ' 'Ignore any error caused by trying to delete 'a style whose name begins with a space On Error Resume Next .Delete On Error GoTo 0 End With End If End With End If Next intStyle 'Display summary MsgBox "There are " & ActiveDocument.Styles.Count - intBuiltInStyles & _ " custom style(s) left in this document.", vbInformation, "Unused Styles Deleted" End If End Sub
[Links last checked February 2022]