![]() |
|
#1
|
|||
|
|||
![]()
I have tried to use the macro below to delete unused styles in my document, however it is also deleting the header and footer styles as well as all the styles inside textboxes. It is not that the macro is doing the wrong thing, the header and footer and textbox styles in the document are listed as 'Not Currently Used" in the style panel, even though they are most definitely used in the document.
![]() This is the macro: https://word.tips.net/T001337_Removi...ed_Styles.html I have read on other forums that I should turn formatting tracking on for those textbox and header/footer styles to be recognized as used, but this doesn't work. Does anyone know how I make Word recognize these styles, or alter the macro so it doesn't delete them? |
#2
|
||||
|
||||
![]()
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
![]()
The macro in the link failed to delete any unused styles. The one in my original post worked to delete unused styles, but also deleted header and footer and text box styles.
|
#4
|
||||
|
||||
![]()
As noted in the link you cannot delete linked or built-in Styles. Other than that, the code does delete unused Styles.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
![]()
Sorry, the macro I tried was the second one. However I just tried running the first and am receiving a runtime error:
![]() Would you have any idea why this is showing? |
#6
|
||||
|
||||
![]()
Try inserting:
On Error Resume Next before: Set Doc = ActiveDocument
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
![]()
Okay thanks, it runs now. However it is deleting my header and footer and text box styles. Those styles have now reverted back to 'Normal'.
|
#8
|
||||
|
||||
![]()
Try:
Code:
Sub DeleteUnusedStyles() Dim Doc As Document, Rng As Range, Shp As Shape Dim StlNm As String, i As Long, bDel As Boolean Application.ScreenUpdating = False On Error Resume Next Set Doc = ActiveDocument With Doc For i = .Styles.Count To 1 Step -1 With .Styles(i) If .BuiltIn = False And .Linked = False Then bDel = True: StlNm = .NameLocal For Each Rng In Doc.StoryRanges With Rng.Find .ClearFormatting .Format = True .Style = StlNm .Execute If .Found = True Then bDel = False Exit For End If End With For Each Shp In Rng.ShapeRange If Not Shp.TextFrame Is Nothing Then With Shp.TextFrame.TextRange.Find .ClearFormatting .Format = True .Style = StlNm .Execute If .Found = True Then bDel = False Exit For End If End With End If Next Next If bDel = True Then .Delete End If End With Next End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
![]()
Thanks for that Macropod, that seems to work (at least what I have tried on my home computer). I will test on my work document tomorrow and let you know how it goes.
Cheers |
#10
|
|||
|
|||
![]()
I have tried the macro at work here and it ran for a couple of hours. It didn't seem to do anything though. I still have all the styles that are not used in the document.
|
#11
|
||||
|
||||
![]()
All what Styles? As I've said a couple of times now (here and in the linked thread), you cannot delete linked or built-in Styles.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#12
|
|||
|
|||
![]()
I don't know what styles they are. When I select 'In use' from the style options list, they are the ones I want to keep. I want to delete any others (apart from of course built in styles). This macro does not do that.
|
#13
|
||||
|
||||
![]()
Perhaps, then, the other Styles are linked. Without seeing a document containing the Styles concerned, it's impossible for me to diagnose the issue.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#14
|
|||
|
|||
![]()
I have experienced a similar problem in the past. It is possible to delete linked styles but first you have to unlink the underlying paragraph and character styles.
This is the code I used to do brute force unlinking as I don't allow the use of linked styles in my documents. IMHO they are an abomination, especially from a VBA perspective. Code:
Sub sbUnLinkAllParagraphCharStyles() ' The enumeration for .type lies. So it is necessary to check if a paragraph style is linked as .type never returns ' wdStyleParagraphLinked as a value ' Dim WordStyle As Style log "sbUnLinkAllParagraphCharStyles" For Each WordStyle In ActiveDocument.Styles If WordStyle.Type = wdStyleTypeParagraph Then With WordStyle If .Linked Then .LinkStyle = ActiveDocument.Styles(wdStyleNormal) ActiveDocument.Styles(WordStyle & " Char").Delete End If End With End If Next End Sub Another point to realise is that you cannot use the 'inuse' flag to determine if a style is in use or not. This flag just remembers if in that document the style has ever been used not what the current status is. To correctly delete unused styles in the manner you wish you have to search the document for an actual occurrence of the style in the list of document styles and then if not found you can delete it with the caveats discussed in the thread above. |
#15
|
|||
|
|||
![]()
Sorry, I should have also said that you can give the appearance of deleting builtin styles by setting the visibility flag of the style to TRUE. The visibility flag has the opposite sense to what you expect because it actually reflects whether or not the style is hidden in the style task pane (Hidden = true = not visible)
Recommended styles = not hidden styles = visible styles. |
![]() |
Tags |
delete unused, macro, styles |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
How to delete unused cell remaining at end of a block of deleted data | SKEETER | Excel | 2 | 10-24-2017 07:06 AM |
![]() |
John9210 | Word VBA | 1 | 02-06-2015 04:47 PM |
Remove unused bottom white space. | johnatanasoff | Word | 4 | 11-05-2014 06:07 PM |
transfering outlook PST to a new pc, unused storage folder and suggested contacts | burgers | Outlook | 1 | 06-14-2011 04:05 PM |
Unused to Outlook need to save folders - help? | franontheedge | Outlook | 0 | 10-31-2007 07:47 AM |