Consulting

Results 1 to 5 of 5

Thread: Solved: Character Styles

  1. #1

    Solved: Character Styles

    Is there a way in Word 2007 to get rid of all of the character styles but keep the formatting (eg. italics or bold)? Thanks.

  2. #2
    When I record a macro to delete the style the code does not get recorded. Anyone know the vba code to delete a character style?

  3. #3
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    ActiveDocument.Styles("Strong").Delete will delete the Strong character style from your document (even though a style is built-in, you can still delete it).

    However, that will remove not only the application of the style, but the formatting you want to retain.

    This is somewhat complicated, because it is such a broad question. The real answer to your question would be to do something along the lines of the following (and you can record a macro to do this):

    1. Replace all instances of a character style with the formatting you want (so you could find a character style which has Bold/Small Caps applied, and replace with the Direct Formatting of Bold/Small Caps in a single fell-swoop.
    2. Delete the character style from the document. The recorded macro from that would look something like this:
    [VBA]
    Sub Macro8()
    '
    ' Macro8 Macro
    '
    '
    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("Intense Reference")
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find.Replacement.Font
    .Bold = True
    .SmallCaps = True
    .AllCaps = False
    End With
    With Selection.Find
    .text = ""
    .Replacement.text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    ActiveDocument.Styles("Intense Reference").Delete
    End Sub
    [/VBA]
    This can obviously be cleaned up a good bit.

    However, the problem is this-- how are you going to determine which formatting to apply to which character style?

    If you're looking for code to:
    1. For each character styles applied in a document
    2. Identify all formatting in a character style
    3. Build your Find/Replace based on that character style
    4. Perform the Find/Replace
    5. Delete the character style
    6. Go to the next character style applied in the document

    That's going to be pretty complex to achieve exactly the document you already have... which begs the question: why do you want to do this? Or is the process a bit simpler? If it's simpler, can you elucidate so that a better answer can be given?

  4. #4
    VBAX Regular
    Joined
    Jan 2011
    Posts
    82
    Location
    Try the following code which removes the applied styles such as 'Heading 2' but retains formatting such as bold and italic which had been applied prior to the style.
    [VBA]Sub RemoveStyles()
    Dim oRng As Word.Range
    Set oRng = ActiveDocument.Range
    oRng.Style = ActiveDocument.styles("Normal")
    End Sub[/VBA]

  5. #5

    Replacing Styles

    By first having the macro find the style and replace it with the font attributes and then deleting the style it seems to work. I was puzzled as to why the macro would not record deleting the styles but after I typed in the code to delete the style it worked. Thanks again for your help!

    Quote Originally Posted by Frosty
    ActiveDocument.Styles("Strong").Delete will delete the Strong character style from your document (even though a style is built-in, you can still delete it).

    However, that will remove not only the application of the style, but the formatting you want to retain.

    This is somewhat complicated, because it is such a broad question. The real answer to your question would be to do something along the lines of the following (and you can record a macro to do this):

    1. Replace all instances of a character style with the formatting you want (so you could find a character style which has Bold/Small Caps applied, and replace with the Direct Formatting of Bold/Small Caps in a single fell-swoop.
    2. Delete the character style from the document. The recorded macro from that would look something like this:
    [vba]
    Sub Macro8()
    '
    ' Macro8 Macro
    '
    '
    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("Intense Reference")
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find.Replacement.Font
    .Bold = True
    .SmallCaps = True
    .AllCaps = False
    End With
    With Selection.Find
    .text = ""
    .Replacement.text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    ActiveDocument.Styles("Intense Reference").Delete
    End Sub
    [/vba]
    This can obviously be cleaned up a good bit.

    However, the problem is this-- how are you going to determine which formatting to apply to which character style?

    If you're looking for code to:
    1. For each character styles applied in a document
    2. Identify all formatting in a character style
    3. Build your Find/Replace based on that character style
    4. Perform the Find/Replace
    5. Delete the character style
    6. Go to the next character style applied in the document

    That's going to be pretty complex to achieve exactly the document you already have... which begs the question: why do you want to do this? Or is the process a bit simpler? If it's simpler, can you elucidate so that a better answer can be given?

Posting Permissions

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