Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-28-2017, 04:33 PM
thejollyroger thejollyroger is offline Macro that deletes unused styles Windows 7 64bit Macro that deletes unused styles Office 2013
Novice
Macro that deletes unused styles
 
Join Date: Nov 2017
Posts: 9
thejollyroger is on a distinguished road
Default Macro that deletes unused styles

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?
Reply With Quote
  #2  
Old 11-29-2017, 04:58 AM
macropod's Avatar
macropod macropod is offline Macro that deletes unused styles Windows 7 64bit Macro that deletes unused styles Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

See: https://www.msofficeforums.com/word-...html#post68044
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 11-29-2017, 03:17 PM
thejollyroger thejollyroger is offline Macro that deletes unused styles Windows 7 64bit Macro that deletes unused styles Office 2013
Novice
Macro that deletes unused styles
 
Join Date: Nov 2017
Posts: 9
thejollyroger is on a distinguished road
Default

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.
Reply With Quote
  #4  
Old 11-29-2017, 03:23 PM
macropod's Avatar
macropod macropod is offline Macro that deletes unused styles Windows 7 64bit Macro that deletes unused styles Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #5  
Old 11-29-2017, 05:28 PM
thejollyroger thejollyroger is offline Macro that deletes unused styles Windows 7 64bit Macro that deletes unused styles Office 2013
Novice
Macro that deletes unused styles
 
Join Date: Nov 2017
Posts: 9
thejollyroger is on a distinguished road
Default

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?
Reply With Quote
  #6  
Old 11-29-2017, 05:31 PM
macropod's Avatar
macropod macropod is offline Macro that deletes unused styles Windows 7 64bit Macro that deletes unused styles Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try inserting:
On Error Resume Next
before:
Set Doc = ActiveDocument
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 11-29-2017, 05:57 PM
thejollyroger thejollyroger is offline Macro that deletes unused styles Windows 7 64bit Macro that deletes unused styles Office 2013
Novice
Macro that deletes unused styles
 
Join Date: Nov 2017
Posts: 9
thejollyroger is on a distinguished road
Default

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'.
Reply With Quote
  #8  
Old 11-29-2017, 09:14 PM
macropod's Avatar
macropod macropod is offline Macro that deletes unused styles Windows 7 64bit Macro that deletes unused styles Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #9  
Old 11-30-2017, 04:52 AM
thejollyroger thejollyroger is offline Macro that deletes unused styles Windows 7 64bit Macro that deletes unused styles Office 2013
Novice
Macro that deletes unused styles
 
Join Date: Nov 2017
Posts: 9
thejollyroger is on a distinguished road
Default

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
Reply With Quote
  #10  
Old 11-30-2017, 05:53 PM
thejollyroger thejollyroger is offline Macro that deletes unused styles Windows 7 64bit Macro that deletes unused styles Office 2013
Novice
Macro that deletes unused styles
 
Join Date: Nov 2017
Posts: 9
thejollyroger is on a distinguished road
Default

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.
Reply With Quote
  #11  
Old 11-30-2017, 08:06 PM
macropod's Avatar
macropod macropod is offline Macro that deletes unused styles Windows 7 64bit Macro that deletes unused styles Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by thejollyroger View Post
I still have all the styles that are not used in the document.
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]
Reply With Quote
  #12  
Old 12-01-2017, 02:19 AM
thejollyroger thejollyroger is offline Macro that deletes unused styles Windows 7 64bit Macro that deletes unused styles Office 2013
Novice
Macro that deletes unused styles
 
Join Date: Nov 2017
Posts: 9
thejollyroger is on a distinguished road
Default

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.
Reply With Quote
  #13  
Old 12-01-2017, 04:03 AM
macropod's Avatar
macropod macropod is offline Macro that deletes unused styles Windows 7 64bit Macro that deletes unused styles Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #14  
Old 12-04-2017, 08:55 AM
slaycock slaycock is offline Macro that deletes unused styles Windows 7 64bit Macro that deletes unused styles Office 2016
Expert
 
Join Date: Sep 2013
Posts: 256
slaycock is on a distinguished road
Default

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
I hope this points you in the right direction.

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.
Reply With Quote
  #15  
Old 12-04-2017, 09:00 AM
slaycock slaycock is offline Macro that deletes unused styles Windows 7 64bit Macro that deletes unused styles Office 2016
Expert
 
Join Date: Sep 2013
Posts: 256
slaycock is on a distinguished road
Default

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.
Reply With Quote
Reply

Tags
delete unused, macro, styles

Thread Tools
Display Modes


Similar Threads
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
Macro that deletes unused styles Macro to Import Styles 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

Other Forums: Access Forums

All times are GMT -7. The time now is 02:43 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft