Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Lounger
    Join Date
    Apr 2004
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Delete All Character Styles (VBA Word 2003)

    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

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Delete All Character Styles (VBA Word 2003)

    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

  3. #3
    Lounger
    Join Date
    Apr 2004
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Delete All Character Styles (VBA Word 2003)

    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

  4. #4
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Delete All Character Styles (VBA Word 2003)

    Jerry,

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

    StuartR

  5. #5
    Lounger
    Join Date
    Apr 2004
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Delete All Character Styles (VBA Word 2003)

    Stuart:

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

    Thanks.
    Jerry

  6. #6
    Lounger
    Join Date
    Apr 2004
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Delete All Character Styles (VBA Word 2003)

    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

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Delete All Character Styles (VBA Word 2003)

    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
    <code>
    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
    </code>
    The line <code>On Error Resume Next</code> simply suppresses the error message

  8. #8
    Lounger
    Join Date
    Apr 2004
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Delete All Character Styles (VBA Word 2003)

    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

  9. #9
    Lounger
    Join Date
    Apr 2004
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Delete All Character Styles (VBA Word 2003)

    I meant to say "not seen as StyleTypeCharacter by Word."

    Jerry

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Delete All Character Styles (VBA Word 2003)

    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.

  11. #11
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Delete All Character Styles (VBA Word 2003)

    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
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  12. #12
    Lounger
    Join Date
    Apr 2005
    Location
    Silver Spring, Maryland, USA
    Posts
    36
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Delete All Character Styles (VBA Word 2003)

    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.

  13. #13
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Delete All Character Styles (VBA Word 2003)

    Sorry, where do you see "that number"?

  14. #14
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts

    Re: Delete All Character Styles (VBA Word 2003)

    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
    [MS MVP - Word]

  15. #15
    Lounger
    Join Date
    Apr 2005
    Location
    Silver Spring, Maryland, USA
    Posts
    36
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Delete All Character Styles (VBA Word 2003)

    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,

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •