Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-31-2014, 10:00 PM
freda0255 freda0255 is offline Style in Use Macro-Need Help Optimizing Code Windows 7 64bit Style in Use Macro-Need Help Optimizing Code Office 2010 64bit
Novice
Style in Use Macro-Need Help Optimizing Code
 
Join Date: Jun 2011
Location: Chicago
Posts: 11
freda0255 is on a distinguished road
Default Style in Use Macro-Need Help Optimizing Code


I found and modified a macro that displays the styles that are really in use in a document.

As I understand it, Word's built-in command to display "styles in use" will display a style if the style was ever in use in a document, even if the style is not currently in use.

I am asking for help in making the macro run faster. The goal is to make it easier for users to select the styles they need by cleaning up the style pane. The users at my company will need to run the macro many times per day. Currently, the macro takes about 30 seconds to run on a 10 page document.

Thank you for your assistance.

Freda
Code:
Sub StylesInUseForReal() 
    Dim oStyle As Style 
    Dim sStyle As String 
    Dim actDoc As Document 
    Dim newDoc As Document 
    Dim sect As section 
    Dim rng As Range 
    Dim sName As String 
    Set actDoc = ActiveDocument 
    sName = actDoc.FullName 
    StatusBar = "Analyzing syles in use! Please be patient! This may take some time." & _ 
    "Hit Ctrl-Break to stop the macro." 
    On Error Resume Next 
    For Each oStyle In actDoc.Styles 
        If oStyle.InUse And IsStyleInUseInDoc(oStyle, actDoc) Then 
            oStyle.Visibility = False 
        End If 
    Next oStyle 
    StyleShowStyleInUse 
    MsgBox "Styles in Use Macro is Complete." 
End Sub 
 
Function IsStyleInUseInDoc(ByVal oStyle As Style, ByVal oDoc As Document) As Boolean 
    Dim oRange As Range 
    Dim bReturn As Boolean 
    bReturn = False 
    For Each oRange In oDoc.StoryRanges 
        If IsStyleInRange(oStyle, oRange) = True Then 
            bReturn = True 
        End If 
    Next oRange 
    IsStyleInUseInDoc = bReturn 
End Function 

 Function IsStyleInRange(ByVal oStyle As Style, ByVal oRange As Range) As Boolean 
    With oRange.Find 
        .ClearFormatting 
        .Style = oStyle 
        .Forward = True 
        .Format = True 
        .Text = "" 
        .Execute 
    End With 
    If oRange.Find.Found = True Then 
        IsStyleInRange = True 
    Else 
        IsStyleInRange = False 
    End If 
End Function 
 
Sub StyleShowStyleInUse() 
    ActiveDocument.FormattingShowFont = False 
    ActiveDocument.FormattingShowParagraph = False 
    ActiveDocument.FormattingShowNumbering = False 
    ActiveDocument.FormattingShowNextLevel = False 
    ActiveDocument.FormattingShowFilter = wdShowFilterStylesInUse 
    ActiveDocument.StyleSortMethod = wdStyleSortByName 
End Sub

Last edited by macropod; 08-01-2014 at 02:16 AM. Reason: Added code tags & formatting
Reply With Quote
  #2  
Old 08-01-2014, 02:28 AM
macropod's Avatar
macropod macropod is offline Style in Use Macro-Need Help Optimizing Code Windows 7 32bit Style in Use Macro-Need Help Optimizing Code Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

Since the only Styles you can delete from a document are user-defined Styles that aren't linked, I'm not sure that a macro that doesn't differentiate between these and unused built-in or linked user-defined Styles will be of much use.

The following macro takes a different approach, automatically deleting any unused unlinked user-defined Styles. Although this will save you the effort of manually deleting the un used Styles that can be deleted, it will still take some time to do all its processing.
Code:
Sub DeleteUnusedStyles()
Dim Doc As Document, bDel As Boolean
Dim Rng As Range, StlNm As String, i As Long
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
            With .Find
              .ClearFormatting
              .Format = True
              .Style = StlNm
              .Execute
            End With
            If .Find.Found = True Then
              bDel = False
              Exit For
            End If
          End With
        Next
        If bDel = True Then .Delete
      End If
    End With
  Next
End With
Application.ScreenUpdating = True
End Sub
PS: When posting code, please use the code tags. They're on the 'Go Advanced' tab at the bottom of this screen.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 08-02-2014, 11:23 AM
freda0255 freda0255 is offline Style in Use Macro-Need Help Optimizing Code Windows 7 64bit Style in Use Macro-Need Help Optimizing Code Office 2010 64bit
Novice
Style in Use Macro-Need Help Optimizing Code
 
Join Date: Jun 2011
Location: Chicago
Posts: 11
freda0255 is on a distinguished road
Default

Hello Paul,

Thank you very much for looking at this code. I appreciate your help very much.

I only wanted to hide the styles and not delete them in case some of the styles were part of a numbering scheme. Could this code be modified to exclude from deletion styles that are linked to numbering?

We use numbering linked to styles extensively. We usually link numbering to styles Heading 1 through Heading 9. However, if we need multiple numbering schemes in a document, we will use a program to create a new numbering scheme which creates 9 styles (e.g., Legal_L1, through Legal_L9) and link all 9 levels of numbering to those 9 style names.

Is there a way to delete unused custom styles but retain the style if it is linked to numbering?

Reply With Quote
  #4  
Old 08-02-2014, 09:40 PM
macropod's Avatar
macropod macropod is offline Style in Use Macro-Need Help Optimizing Code Windows 7 32bit Style in Use Macro-Need Help Optimizing Code Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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 indicated in my previous post, with that macro you can't delete linked Styles or Styles that are in use, so it seems that, apart from deleting as opposed to merely hiding Styles, it does what you want. To merely hide the unused Styles instead of deleting them, regarless of whether they're built-in or linked, you could use:
Code:
Sub HideUnusedStyles()
Dim Doc As Document, bDel As Boolean
Dim Rng As Range, StlNm As String, i As Long
Application.ScreenUpdating = False
On Error Resume Next
Set Doc = ActiveDocument
With Doc
  For i = .Styles.Count To 1 Step -1
    With .Styles(i)
        bDel = True: StlNm = .NameLocal
        For Each Rng In Doc.StoryRanges
          With Rng
            With .Find
              .ClearFormatting
              .Format = True
              .Style = StlNm
              .Execute
            End With
            If .Find.Found = True Then
              bDel = False
              Exit For
            End If
          End With
        Next
        If bDel = True Then .Visibility = False
    End With
  Next
End With
Application.ScreenUpdating = True
End Sub
The only material difference between this sub and the previous one is that I've omitted the 'If .BuiltIn = False And .Linked = False Then' test and I've changed:
.Delete
to:
.Visibility = False
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 08-04-2014, 07:35 PM
freda0255 freda0255 is offline Style in Use Macro-Need Help Optimizing Code Windows 7 64bit Style in Use Macro-Need Help Optimizing Code Office 2010 64bit
Novice
Style in Use Macro-Need Help Optimizing Code
 
Join Date: Jun 2011
Location: Chicago
Posts: 11
freda0255 is on a distinguished road
Default

Thank you once again Paul. I am going to study your code and work to adapt it to this particular problem. I am very grateful for your assistance.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Style in Use Macro-Need Help Optimizing Code optimizing computer for MS Office shmu Misc 4 02-18-2014 12:34 AM
Style in Use Macro-Need Help Optimizing Code VBA Code for adding Macro to QAT OTPM Project 4 08-22-2013 01:33 PM
Need Macro code for Outlook gbaker Outlook 0 04-11-2013 10:29 AM
Style in Use Macro-Need Help Optimizing Code I need help with macro...code needed for automatic sorting chefmate Excel Programming 1 08-26-2012 01:04 AM
Macro to Table Style ubns Word VBA 1 04-16-2012 05:01 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:58 AM.


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