macro to remove manually-applied character formatting

J

John

I'd like to write a macro that removes all character formatting of
the selected text (eg manually applied italics). It would be similar
to the fuction that Ctrl + Spacebar performs, except that this also
removes the formatting applied by character styles (eg the blue text
and underlining of the hyperlink character style). I want to preserve
the formatting of these character styles. Perhaps there is a way to do
this in Word without using a macro?
 
S

Stefan Blom

The following macro seems to work:

Sub DelDirectFormatting()
Dim c As Range
For Each c In Selection.Characters
If c.Style.Type <> wdStyleTypeCharacter Then
c.Font.Reset
End If
Next c
End Sub
 
K

Klaus Linke

If you also want to remove manual character formatting that is applied on top of the character style, the macro becomes a bit longer:

Sub ResetCharKlaus()
Dim myChar As Range
Dim myStyle As Style
Dim myParaStyle As Style
Application.ScreenUpdating = False
For Each myChar In Selection.Characters
Set myStyle = myChar.Style
Set myParaStyle = myChar.Paragraphs(1).Style
If myStyle <> myParaStyle Then
myChar.Style = myStyle
Else
myChar.Style = wdStyleDefaultParagraphFont
End If
Next myChar
Application.ScreenUpdating = True
End Sub

If you rename it to ResetChar, it'll be called automatically when you use Ctrl+Spacebar.

Regards,
Klaus
 
S

Stefan Blom

Excellent solution! This is how the ResetChar command should work, in
my opinion.

--
Stefan Blom
Microsoft Word MVP
Excellent solution!

--
Stefan Blom
message If you also want to remove manual character formatting that is applied
on top of the character style, the macro becomes a bit longer:

Sub ResetCharKlaus()
Dim myChar As Range
Dim myStyle As Style
Dim myParaStyle As Style
Application.ScreenUpdating = False
For Each myChar In Selection.Characters
Set myStyle = myChar.Style
Set myParaStyle = myChar.Paragraphs(1).Style
If myStyle <> myParaStyle Then
myChar.Style = myStyle
Else
myChar.Style = wdStyleDefaultParagraphFont
End If
Next myChar
Application.ScreenUpdating = True
End Sub

If you rename it to ResetChar, it'll be called automatically when you
use Ctrl+Spacebar.

Regards,
Klaus
 
J

John

Thanks Klaus, that works, but I'm confused about line 10:
myChar.Style = myStyle

This seems to be the same as
myChar.Style = myChar.Style
which would appear to do nothing. But if I remove it (by commenting it
out) it no longer works properly - manually applied formatting on a
character style is no longer removed.

If I replace your line 10 with
myChar.Style = myChar.Style
ie, assigning the variable to the same variable, it works again -
manually applied formatting is again removed.

Could someone explain what's going on please?

Thanks again everyone for all your help...


If you also want to remove manual character formatting that is applied
on top of the character style, the macro becomes a bit longer:

Sub ResetCharKlaus()
Dim myChar As Range
Dim myStyle As Style
Dim myParaStyle As Style
Application.ScreenUpdating = False
For Each myChar In Selection.Characters
Set myStyle = myChar.Style
Set myParaStyle = myChar.Paragraphs(1).Style
If myStyle <> myParaStyle Then
myChar.Style = myStyle
Else
myChar.Style = wdStyleDefaultParagraphFont
End If
Next myChar
Application.ScreenUpdating = True
End Sub

If you rename it to ResetChar, it'll be called automatically when you
use Ctrl+Spacebar.

Regards,
Klaus
 
K

Klaus Linke

myChar.Style = myStyle


Yes, that looks weird all right.

It's just one of Word's peculiarities: (Re-)Applying a character style will
remove any manual character formatting.
And that is what you want the macro to do.

Analogous, (re-)applying a paragraph style would remove all manual paragraph
formatting.
It'll usually keep character formatting and character styles, but there are
some exceptions and special rules about that. It depends on whether you have
one paragraph or more selected, and on whether more or less than 50% of the
characters in the paragraph are manually formatted.

BTW, instead of
myChar.Style = wdStyleDefaultParagraphFont
I could probably have used Stefan's
myChar.Font.Reset
which might be easier to read.

Regards,
Klaus
 
J

John

Thanks for your explanation, it makes sense now.

I tried using myChar.Font.Reset, but it when applying a macro to a
whole page of Word text on a slow system (Windows NT, Pentium 2,
266MHz, Word 97) it took about a minute to execute. Your original
version was faster, but still fairly slow.

I'd like to make a few enhancements, and am looking for ideas since my
knowlege of VBA is basic.

1) Any ideas for speed-ups? Could I make use of VBA find/replace to
search for character styles? If so, this would process many rather
than just one character at a time in the loop.

2) When manually-applied subscripts/superscripts are found, I'd like
it to remove the manual formatting (eg font size) but maintain the
subscripts/superscripts manual formatting for these characters.

3) When manually-applied symbol font is found (eg for Greek
characters) I'd like to remove the manual formatting (eg font size)
but maintain the symbol font manual formatting for these characters.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top