PDA

View Full Version : Solved: Character Styles



Bernadette
09-15-2011, 08:36 AM
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.

Bernadette
09-16-2011, 08:17 AM
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?

Frosty
09-16-2011, 03:26 PM
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:

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

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?

Talis
09-17-2011, 12:28 PM
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.
Sub RemoveStyles()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
oRng.Style = ActiveDocument.styles("Normal")
End Sub

Bernadette
09-19-2011, 08:12 AM
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!


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:

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

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?