• Delete All Character Styles (VBA Word 2003)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Delete All Character Styles (VBA Word 2003)

    Author
    Topic
    #451475

    I found this code on the internet. I thought it would delete all of the character styles in a document. It doesn’t. Does anyone know a way to do that?
    Thanks.
    Jerry

    Dim aStl As Style
    Dim rDcm As Range
    Set rDcm = ActiveDocument.Range
    For Each aStl In ActiveDocument.styles
    If aStl.Type = wdStyleTypeCharacter And aStl “Default Paragraph Font” Then
    With rDcm.Find
    .Style = aStl
    While .Execute
    rDcm.Select
    ActiveDocument.styles(aStl).Delete
    Wend
    End With
    End If
    Next

    Viewing 0 reply threads
    Author
    Replies
    • #1111557

      The code you posted will delete all character styles that are actually used in the document. It won’t delete character styles that are not in use. To delete *all* character styles (except the pseudo-style “Default Paragraph Font”) you can use

      Dim aStl As Style
      For Each aStl In ActiveDocument.Styles
      If aStl.Type = wdStyleTypeCharacter And aStl “Default Paragraph Font” Then
      ActiveDocument.Styles(aStl).Delete
      End If
      Next aStl

      • #1111606

        Hans:

        I was hoping it would do that—delete all of the character styles being used—but it misses a lot of them for some reason.
        Deleting all available character styles would be great. I will try that.

        Thanks.
        Jerry

        • #1111658

          Jerry,

          Do you really want to “delete the styles” or do you want to remove the character style formatting from the text?

          StuartR

          • #1111661

            Stuart:

            Actually I want to remove all of them, even the unused ones.
            Han’s code works great.

            Thanks.
            Jerry

      • #1112081

        Hans:

        Me again.
        Under what circumstances might this macro fail. With certain files I get an error message at the line ActiveDocument.Styles(aStl).Delete saying that the style cannot be found, but the file has plenty of character styles in it. Any ideas?

        Thanks.
        Jerry

        • #1112082

          As far as I can tell, the Styles collection includes the styles that are built into Word, such as Hyperlink and Page Number. These built-in styles cannot be removed. You could change the code to

          Sub RemoveCharStyles()
          Dim aStl As Style
          On Error Resume Next
          For Each aStl In ActiveDocument.Styles
          If aStl.Type = wdStyleTypeCharacter Then
          ActiveDocument.Styles(aStl).Delete
          End If
          Next aStl
          End Sub

          The line On Error Resume Next simply suppresses the error message

          • #1112136

            Hans:

            Thanks. That does remove some of the character styles from the file, but not all. I know that Word can generate its own character styles from paragraph styles. I was wondering if those are the ones that get left behind for some reason. I could from the names that they are not “built-in” styles, but styles that started out as paragraph styles and became charcter styles at some point. So I added this to the macro:

            If aStl.Type = wdStyleTypeParagraph Then
            ActiveDocument.styles(aStl).Delete
            End If

            That did remove all of the problem character styles that were left behind, but of course wiped out all of paragraph styles too. I guess the character styles that are based on paragraph styles are not seen as StyleTypeParagraph by Word.

            Jerry

            • #1112137

              I meant to say “not seen as StyleTypeCharacter by Word.”

              Jerry

            • #1112147

              If “Keep track of formatting” has been ticked in the Edit tab of Tools | Options…, Word displays pseudo-styles based on formatting. For example if you make a word bold in a paragraph that has been formatted as List Number, Word will display a pseudo-style named List Number+Bold. Such pseudo-styles are listed as paragraph styles, so you can’t delete them even if they act like paragraph styles.
              For this and related reasons I always turn “Keep track of formatting” off. The only disdvantage is that the option “Select All” in the Styles and Formatting task pane is disabled, but I can live with that.

            • #1118126

              It’s never been clear to me what that number counts, and what gets selected. I have instances where the use count lists, say, 21 instances, but only 18 or so are visible. Asking Word to find the instances only shows the 18 visible ones. Yes, I am including all the options in the Find box to look at the main doc, the text frames, and the headers/footers.

              Does anybody know about this? I’ve had documents that insist there are 38 instances of Normal style, then refuse to find any of them.

              Thanks.

            • #1118129

              Sorry, where do you see “that number”?

            • #1118280

              Sorry – I mean the number that shows up when Word offers to highlight all XX instances of the style (if you have Keep Track of formatting selected for Word 2003). XX seems much too high to me, especially for Normal style. Word will offer to highlight all 23 instances in use, but only one or two are actually highlighted. Those two are the same that are found if I ask to find text with that style. The others seem to be phantoms of some kind.

              Yes, I use find on the headers and footers too, so that doesn’t account for the discrepancy.

              Thanks,

            • #1118300

              Aha! After tracking through lots of other posts, I found that the extra instances are probably end-of-row markers in tables. I don’t know why the end-of-row markers should have Normal style, but they seem to. Even though when I select them the style shows as either the table style or the paragraph style of the adjacent paragraph, running macros that examine each paragraph and record its style indicate that those end of row markers get picked up as Normal style.

              Using Andrew Savikas’ method (in Post: 462,899) for figuring out whether the current paragraph is an end of row marker I was able to skip them in the style counter – and nearly all instances of Normal disappeared.

              Now to find a way to detect whether a paragraph is an empty paragraph in a header or footer. The character count property for paragraphs in a header or footer with no text is 1.

              Thanks,

            • #1112246

              Jerry, can you explain to us WHY exactly you want to delete all the character styles from the document. This might help us come up with a solution that better suits your aim.

              I get the feeling that you are only trying to remove all the formatting from within paragraphs (both character styling and local formatting) and keep only the paragraph style formatting. This can be done by selecting all (Ctrl-A) and pressing Ctrl-Space and Ctrl-Q

            • #1118246

              Hi Jerry,

              There’s a bunch of other built-in character styles you can’t delete also. These include:
              Comment Reference
              Emphasis
              Endnote Reference
              Followed Hyperlink
              Footnote Reference
              HTML Acronym
              HTML Address
              HTML Cite
              HTML Code
              HTML Definition
              HTML Keyboard
              HTML Sample
              HTML Typewriter
              HTML Variable
              Line Number
              Page Number
              Strong

              This list is for Word 2000. Later versions may have more.

              Cheers,
              Paul Edstein
              [Fmr MS MVP - Word]

    Viewing 0 reply threads
    Reply To: Delete All Character Styles (VBA Word 2003)

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

    Your information: