none
Deleting unused styles in MS Word 2013 RRS feed

  • Question

  • Hello,

    I have tried to use the following code to remove unused styles. Sadly, it has mashed my document base by deleting some styles that are in use. I cannot yet comprehend the pattern of which in-use styles the code deletes and which it does not.

    Can anyone see any errors in the below code, or does anyone have any ideas for a better way to achieve this?

    Many thanks.

    Keith

     

                  Public Shared Sub F_S_DeleteUnusedStyles(ByVal V_V_Scalar_Document_Generic As Document)

                         For Each L_V_Scalar_Style_Generic As Style In V_V_Scalar_Document_Generic.Styles

                               Try

                                      If Not L_V_Scalar_Style_Generic.BuiltIn Then

                                             With V_V_Scalar_Document_Generic.Content.Find

                                                    .ClearFormatting()

                                                    .Style = L_V_Scalar_Style_Generic.NameLocal

                                                    .Execute(FindText:=String.Empty, Format:=True)

                                                    If Not .Found Then

                                                           System.Diagnostics.Debug.WriteLine(L_V_Scalar_Style_Generic.NameLocal)

                                                           L_V_Scalar_Style_Generic.Delete()

                                                    End If

                                             End With

                                      End If

                               Catch L_V_Scalar_Exception_Generic As System.Exception

                                      'Swallow this error, since COM fails unpredictably, in a non-fatal way, sometimes.

                                      System.Diagnostics.Debug.WriteLine("File '" & V_V_Scalar_Document_Generic.FullName & "'; Message: '" & L_V_Scalar_Exception_Generic.Message & "'; NameLocal = '" & L_V_Scalar_Style_Generic.NameLocal & "'.", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information, System.Windows.Forms.MessageBoxDefaultButton.Button1)

                               End Try

                         Next

                  End Sub


    R K Howard

    Friday, February 21, 2014 12:03 PM

Answers

  • Yes, that could make a difference, one that can be addressed by changing:
    If .BuiltIn = False Then
    to:
    If .BuiltIn = False And .Linked = False Then

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, February 24, 2014 8:20 PM

All replies

  • With V_V_Scalar_Document_Generic.Content

    Content is only the main document body, not headers, footers, footnotes, content in drawing objects, etc. Any style used in those will be deleted.

    The other thing that could be happening is that the collection you're looping through may be "skipping" (due to deleting, thus throwing the count of the loop off). You might try writing all the style names you want to delete first to an array/dictionary/whatever (this would also give you an opportunity to check what's getting targeted) then loop that in order to delete the styles.

    Why aren't you using the built-in Style.InUse property of Word's object model?


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, February 21, 2014 6:09 PM
    Moderator
  • Try something based on the following vba code:

    Sub KillUnusedStyles()
    Dim Doc As Document, bDel As Boolean
    Dim Rng As Range, StlNm As String, i As Long
    Application.ScreenUpdating = False
    Set Doc = ActiveDocument
    With Doc
      For i = .Styles.Count To 1 Step -1
        With .Styles(i)
          If .BuiltIn = 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


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sunday, February 23, 2014 5:03 AM
  • Hello Paul,

    I tried this but it still deletes used styles. I should point out that it deletes styles with a Style Type of 'Linked (paragraph and character)'. Should that make a difference?

    Many thanks.

    Keith


    R K Howard

    Monday, February 24, 2014 7:13 PM
  • Yes, that could make a difference, one that can be addressed by changing:
    If .BuiltIn = False Then
    to:
    If .BuiltIn = False And .Linked = False Then

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, February 24, 2014 8:20 PM
  • Yes, Paul, that was exactly the problem. Thanks for your help. - Keith

    I used the following logic:

    If .BuiltIn = False Then

        If [style is unused] Then

             If .Linked = False Then

                     Delete the style.


    R K Howard

    Tuesday, February 25, 2014 12:54 PM
  • Hi,Keith

    Could you please send me the full code that worked for you.

    I'm use word 2013 too , and I haven't Vbscript experience.

    Thanks.

    zhangyi7773@gmail.com

    Saturday, June 17, 2017 3:18 AM
  • Eric,

    I wrote the code in Visual Basic Dot Net, not VB Script.

    Here it is...

    Kind regards,

    Keith

    FriendSharedSubF_S_Delete(

    ByValV_V_Scalar_Style_Generic AsStyle,

    ByValV_V_Scalar_Boolean_StyleIsDeletedIfUnused AsBoolean)

    Try


    IfNotV_V_Scalar_Style_Generic.BuiltIn Then


    DimL_V_Scalar_Boolean_StyleIsDeleted AsBoolean


    IfNotV_V_Scalar_Style_Generic.Linked Then


    L_V_Scalar_Boolean_StyleIsDeleted =

    True


    IfV_V_Scalar_Boolean_StyleIsDeletedIfUnused Then


    WithV_V_Scalar_Style_Generic.Application.ActiveDocument.Content.Find

    .ClearFormatting()

    .Style = V_V_Scalar_Style_Generic.NameLocal

    .Execute(FindText:=

    String.Empty, Format:=True)

    If.Found Then


    L_V_Scalar_Boolean_StyleIsDeleted =

    False


    EndIf


    EndWith


    DimL_V_Scalar_Range_Generic AsRange


    ForEachL_V_Scalar_Range_Generic InV_V_Scalar_Style_Generic.Application.ActiveDocument.StoryRanges

    If(L_V_Scalar_Range_Generic.StoryType = WdStoryType.wdPrimaryHeaderStory) OrElse


    (L_V_Scalar_Range_Generic.StoryType =

    WdStoryType.wdPrimaryFooterStory) Then


    WithL_V_Scalar_Range_Generic.Find

    .ClearFormatting()

    .Style = V_V_Scalar_Style_Generic.NameLocal

    .Execute(FindText:=

    String.Empty, Format:=True)

    If.Found Then


    L_V_Scalar_Boolean_StyleIsDeleted =

    False


    ExitFor


    EndIf


    EndWith


    EndIf


    Next


    EndIf


    EndIf


    IfL_V_Scalar_Boolean_StyleIsDeleted Then


    V_V_Scalar_Style_Generic.Delete()

    EndIf


    EndIf


    CatchL_V_Scalar_Exception_Generic AsSystem.Exception


    DimL_V_Scalar_StackTrace_Generic AsSystem.Diagnostics.StackTrace= NewSystem.Diagnostics.StackTrace(True)

    DimL_V_Scalar_StackFrame_Generic AsSystem.Diagnostics.StackFrame= L_V_Scalar_StackTrace_Generic.GetFrame(0)

    C_A_H_CommonCoreCore.

    U_A_G_C_U_Exception.G_S_DisplayOutputAndWriteMessage(

    "This procedure crashed."& System.Environment.NewLine &

    "This may be because the document contains a custom style with a non-conforming style ID."& System.Environment.NewLine &

    "This is a not-fatal error, so execution will now continue."& System.Environment.NewLine &

    "However, you should execute the 'Removing custom styles which have a non-conforming style ID from documents' procedure in the Compliance Manual Form, to fix this problem.",

    L_V_Scalar_Exception_Generic,

    L_V_Scalar_StackFrame_Generic,

    False


    )

    EndTry


    EndSub


    R K Howard

    Monday, June 19, 2017 11:53 AM