• Finding unused styles (Word 97 VBA)

    Author
    Topic
    #358398

    I have tried using the macro below to create a listing of unused styles. This came from WordTips. This helps me when I am cleaning out a document that has 100s of styles. The problem is that when it gives me the three lists (styles used in document, built in styles not used, and user-defined styles not used) it seems to only give me 9 of each, even a document that has 170 styles (according to the Organizer [Format>Styles>Organizer command button]).

    Can anyone see what this is only returning 9 values per list/array?

    Thanks!!
    Troy

    P.S. How do you guys get your macro text to show up in these posts with the proper indents?

    The full article is included below:
    ——————————
    Finding Unused Styles
    ——————————
    The capability to create and manage styles is one of the strong
    features of Word. Styles allow you to quickly and easily apply
    consistent formatting throughout your document, and update that
    formatting as your needs change.

    Word provides quite a few built-in (predefined) styles, and you can
    add more as your needs dictate. At some time you might want to
    determine which styles are not in use in a document. This list could
    then be used to determine which styles you could easily delete, simply
    because they are no longer needed.

    There is no intrinsic way to create an unused style list in Word.
    Instead, you need to create a macro to do the job for you. You might
    think that creating such a macro would be a simple task of looking at
    which styles Word believes are in use, and then comparing those to the
    styles which are defined. The problem with this approach is that VBA’s
    InUse property (which applies to Style objects) is used for several
    purposes. The official definition for the InUse property is that it’s
    True if either of the following two conditions are met:

    * The style is a built-in style that has been modified or applied
    in the document.
    * The style is a user-defined style that has been created in the
    document.

    What that means is that the InUse property doesn’t indicate if a style
    is actually in use in the document. You could do something to the
    definition of a style without actually applying it, and that style
    would be flagged as ‘in use’ even though there isn’t any text in the
    document actually using the style.

    However, it is possible to generate a list of styles not in use by
    using both the InBuilt and InUse properties in a macro. The following
    VBA macro uses this approach:

    Sub CreateStyleList()
    Dim docThis As Document
    Dim styItem As Style
    Dim sBuiltIn(499) As String
    Dim iStyBICount As Integer
    Dim sUserDef(499) As String
    Dim iStyUDCount As Integer
    Dim sInUse(499) As String
    Dim iStyIUCount As Integer
    Dim iParCount As Integer
    Dim J As Integer, K As Integer
    Dim sParStyle As String
    Dim bInUse As Boolean

    ‘ Ref the active document
    Set docThis = ActiveDocument

    ‘ Collect all styles being used
    iStyIUCount = 0
    iParCount = docThis.Paragraphs.Count
    iParOut = 0
    For J = 1 To iParCount
    sParStyle = docThis.Paragraphs(J).Style
    For K = 1 To iStyIUCount
    If sParStyle = sInUse(K) Then Exit For
    Next K
    If K = iStyIUCount + 1 Then
    iStyIUCount = K
    sInUse(iStyIUCount) = sParStyle
    End If
    Next J

    iStyBICount = 0
    iStyUDCount = 0
    ‘ Check out styles that are “in use”
    For Each styItem In docThis.Styles
    ‘see if in those being used
    bInUse = False
    For J = 1 To iStyIUCount
    If styItem.NameLocal = sInUse(J) Then bInUse = True
    Next J
    ‘Add to those not in use
    If Not bInUse Then
    If styItem.BuiltIn Then
    iStyBICount = iStyBICount + 1
    sBuiltIn(iStyBICount) = styItem.NameLocal
    Else
    iStyUDCount = iStyUDCount + 1
    sUserDef(iStyUDCount) = styItem.NameLocal
    End If
    End If
    Next styItem

    ‘Now create the output document
    Documents.Add

    Selection.TypeText “Styles In Use”
    Selection.TypeParagraph
    For J = 1 To iStyIUCount
    Selection.TypeText sInUse(J)
    Selection.TypeParagraph
    Next J
    Selection.TypeParagraph
    Selection.TypeParagraph

    Selection.TypeText “Built-in Styles Not In Use”
    Selection.TypeParagraph
    For J = 1 To iStyIUCount
    Selection.TypeText sBuiltIn(J)
    Selection.TypeParagraph
    Next J
    Selection.TypeParagraph
    Selection.TypeParagraph

    Selection.TypeText “User-defined Styles Not In Use”
    Selection.TypeParagraph
    For J = 1 To iStyIUCount
    Selection.TypeText sUserDef(J)
    Selection.TypeParagraph
    Next J
    Selection.TypeParagraph
    Selection.TypeParagraph
    End Sub

    The macro first examines every paragraph in the document to determine
    the names of the styles actually being used in the document. This
    information is stored in the sInUse array. Then, the macro starts
    looking through the list which Word thinks are in use–these are the
    styles that belong to the Styles collection. If the style is not in
    the sInUse array, then it is added either to the sBuiltIn array (for
    built-in styles) or the sUserDef array (for user-defined styles). When
    the comparisons are done, a new document is created that lists the
    results.

    (Thanks to Graham Smith, Gary Frieder, Gaetano J. Milano, Leonard
    LaPadula, David J. Bookbinder, and Martyn Yeo for their contributions
    to this tip.)

    Viewing 1 reply thread
    Author
    Replies
    • #534720

      Hi Troy,

      I had the same problem with this macro (and others) which was published in the Word Tips newsletter.

      Brief rant: I stopped contributing to the Help Wanted section of the Word Tips newsletter because: several people may have contributed different solutions. One person’s solution is published, but everyone who contributed any answer is credited. This may seem fair, but in this case and a few others, my name was listed, the macro had nothing to do with the one I contributed, and to make matters worse, in a couple of cases the macro published didn’t even work! Meanwhile the one I contributed was (IMO) simpler and had the side benefit that it actually worked! [End rant]

      Well, my name is listed so that means I must have contributed something workable! – I’ll look through my files and try to track it down; will repost if/when found.

      Gary

    • #534723

      Hi again,

      OK, I tracked down the macro I sent to Word Tips and which did not get published.

      [Rant reprise] Have a look at this one, vs. the one that got published. Isn’t this one just a wee bit cleaner? Then factor in that this one works, while the published one doesn’t. And lastly factor in that my name was published as a contributor to the one that doesn’t work! nuts flee

      Anyway, I hope this one works for you (I’ll look pretty dumb if it doesn’t!)

      By the way, use the [ pre ] and [ /pre ] tags (with no spaces after and before the brackets) to get the code to indent.

      Gary

      Public Sub ListUnusedStyles()
      'Purpose: produces a list of user-defined styles that
      ' are not actually in use in the current document
      'Author:  Gary Frieder  2001
      Dim strCurDocName As String
      Dim aStyle As Style
      Dim strStyleName As String
      Dim strNotInUseList As String
      Dim docNewDoc As Document
      strCurDocName = ActiveDocument.Name
      For Each aStyle In ActiveDocument.Styles
          If aStyle.BuiltIn = False Then
              strStyleName = aStyle.NameLocal
              If bIsStyleReallyInUse(strStyleName) = False Then
                  strNotInUseList = strNotInUseList & strStyleName & vbCrLf
              End If
          End If
      Next aStyle
      
      Set docNewDoc = Documents.Add
      docNewDoc.Content = "User-defined Styles not in use in " _
                          & strCurDocName & ": " & vbCrLf & strNotInUseList
      
      End Sub
      '================================
      Public Function bIsStyleReallyInUse(strStyle As String) As Boolean
      
      With ActiveDocument.Content.Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Style = ActiveDocument.Styles(strStyle)
          .Text = ""
          .Replacement.Text = ""
          .Forward = True
          .Wrap = wdFindContinue
          .Format = True
          .MatchCase = False
          .MatchWholeWord = False
          .MatchWildcards = False
          .MatchSoundsLike = False
          .MatchAllWordForms = False
          If .Execute Then
              bIsStyleReallyInUse = True
          End If
      End With
      End Function
      
      • #534940

        Thanks for the response. Sorry you got shafted by WordTips. I was kind of surprised because I have gotten a lot of good information from them. Oh well, I guess noone’s perfect.

        Speaking of which, this macro ran great, EXCEPT:

        It only showed unused user defined styles. I want to also see unused built in styles. Any suggestions/additions to the original macro that could accomplish this?

        Thanks again for your help!!
        Troy

        • #535045

          Hi Troy,

          Sorry if I got a little crabby there! laugh

          You can probably get the macro to include the “built-in styles not actually really in use” by deleting or commenting out the lines of code:

          If aStyle.BuiltIn = False

          AND:

          End If

          That are before and after the:

          strStyleName = aStyle.NameLocal
          If bIsStyleReallyInUse(strStyleName) = False Then
          strNotInUseList = strNotInUseList & strStyleName & vbCrLfEnd If

          bit.

          Let me know if that works OK.

          Gary

          • #535078

            No problem about being crabby. I get that way to sometimes. hairout

            As far as the changes, let me tell you the affect:

            1. The macro now looks through a list of all built in styles in Word, not just the list of built in styles stored in the document. So the of built-in styles included in the output list is much longer now than the number of built-in styles actually stored in the document.

            For example, when I look at the styles stored in my document in Organizer (Format>Styles>Organizer command button), among others I see the following styles that start with “L”

            Labels
            Letter
            List
            List 10+
            List sub
            List+RTab
            List Button
            List Number

            (Some of this are built-in and some are not.)

            The styles found included:

            Labels
            Letter
            Line Number
            List 10+
            List 2
            List 3
            List 4
            List 5
            List Bullet
            List Bullet 2
            List Bullet 3
            List Bullet 4
            List Bullet 5
            List Continue
            List Continue 2
            List Continue 3
            List Continue 4
            List Continue 5
            List Number
            List Number 2
            List Number 3
            List Number 4
            List Number 5
            List sub
            List+RTab
            ListButton
            ListNumber

            2. One other side affect (which I have also seen after trying to use the Find function (CTRL+F) to find a built-in style that actually was not stored in my document) is that now all the built-in styles contained in Word appear in the Organizer as styles stored in this document. That gives me more styles I will have to delete and more work later.

            Hey, here is an interesting thought. After solving the problem mentioned above (and I would like the ability to list the unused styles despite what I am about to ask), could we add a line of code to this macro that would delete these unused styles from the document?

            Thanks again for all your help!!
            Troy

            • #535110

              Hi Troy,

              That defintely is an unexpected result and I need to figure out why that is happening. In the meantime, click here to see a post by Dave Rado on one of the MS Word newsgroups – this contains a macro which does do the trick correctly.

              Gary

            • #535111

              Borrowing a key line of code from Dave Rado’s macro, I’ve got my version now working for custom and built-in styles as well:

              Public Sub ListUnusedStyles()
              'Purpose: produces a list of styles that
              ' are not actually in use in the current document
              'Author:  Gary Frieder  2001
              Dim strCurDocName As String
              Dim aStyle As Style
              Dim strStyleName As String
              Dim strNotInUseList As String
              Dim docNewDoc As Document
              strCurDocName = ActiveDocument.Name
              For Each aStyle In ActiveDocument.Styles
                  If aStyle.InUse And aStyle.NameLocal  "Default Paragraph Font" Then
                      strStyleName = aStyle.NameLocal
                      If bIsStyleReallyInUse(strStyleName) = False Then
                          strNotInUseList = strNotInUseList & strStyleName & vbCrLf
                      End If
                  End If
              Next aStyle
              
              Set docNewDoc = Documents.Add
              docNewDoc.Content = "Styles not in use in " _
                                  & strCurDocName & ": " & vbCrLf & strNotInUseList
              
              End Sub
              '================================
              Public Function bIsStyleReallyInUse(strStyle As String) As Boolean
              
              With ActiveDocument.Content.Find
                  .Style = strStyle
                  .Forward = True
                  .Wrap = wdFindContinue
                  .Format = True
                  If .Execute Then
                      bIsStyleReallyInUse = True
                  End If
              End With
              End Function
              

              So you can take your pick. Dave’s is more thorough in that it tests for styles in all the story ranges of the document, not just the main text story; it has a couple of very minor drawbacks in that it appears to add Header and Footer styles as really in use when they weren’t before, and it may affect the content of previously empty headers/footers. My version treads more lightly in that regard, but is not as thorough in that it doesn’t go into each story range.

            • #535165

              This works beautifully. Thanks for your help!!
              Troy

    Viewing 1 reply thread
    Reply To: Finding unused styles (Word 97 VBA)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: