Remove a character style

User avatar
ErikJan
BronzeLounger
Posts: 1302
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Remove a character style

Post by ErikJan »

For a small project I'm doing, I need to apply a character style to some parts of my document (see another recent thread here).
Until now I did that manually but I know I can do this via VBA. If the user selects the text, I can add this code to a button:

Code: Select all

Selection.Style = ActiveDocument.Styles("MyStyle")
My question is, can I also reverse that? So a user selects some text and clicks another button, how do I specifically remove the "MyStyle" style (whether or not it is applied to -part of- the selected text or not)?

User avatar
HansV
Administrator
Posts: 79045
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Remove a character style

Post by HansV »

Code: Select all

Selection.Font.Reset
will reset all character formatting to that of the paragraph style of the text.
Best wishes,
Hans

User avatar
ErikJan
BronzeLounger
Posts: 1302
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Re: Remove a character style

Post by ErikJan »

OK, but can I also just remove the "MyStyle" style (as I could add it)?

User avatar
HansV
Administrator
Posts: 79045
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Remove a character style

Post by HansV »

I knew you were going to ask this. Not as far as I know, but let's see if someone else has a suggestion.
Best wishes,
Hans

User avatar
StuartR
Administrator
Posts: 12711
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Remove a character style

Post by StuartR »

When you remove the character style what do you expect to happen? Word has no knowledge of any manual formatting that was previously applied to those characters, so the only thing it could do is reapply the character formatting from the underlying paragraph style. That is exactly what

Code: Select all

Selection.Font.Reset
does
StuartR


User avatar
ErikJan
BronzeLounger
Posts: 1302
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Re: Remove a character style

Post by ErikJan »

I think I disagree... If I apply a style, Word knows that as the "Style Inspector" shows that and also, if I select text with a style (or styles) applied and use the drop-down for that style, in the Styles-pane I see e.g. "Modify", but also "Select All x Instance(s)" and "Clear Formatting of x Instance(s)".
So Word knows that this style is applied to this piece of text and it even knows all other places where it was applied.
It can remove the style in all places it was used but -to date- I don't see how to remove it from just the selected text... I'm guessing/hoping that where this might not be possible in the GUI, it would be using VBA

User avatar
HansV
Administrator
Posts: 79045
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Remove a character style

Post by HansV »

Code: Select all

Selection.ClearCharacterStyle
will remove all character styles applied to the selection, but leave directly applied character formatting alone. As far as I know, it doesn't get more specific than that. Word VBA doesn't appear to offer a method for unapplying one character style in particular.
Best wishes,
Hans

User avatar
p45cal
2StarLounger
Posts: 161
Joined: 11 Jun 2012, 20:37

Re: Remove a character style

Post by p45cal »

A guess:

Code: Select all

Selection.Style = ActiveDocument.Styles("Normal")
I opened a new blank Word document, pasted in some plain text then examined what style it was. It was Normal.
I'm not familiar with Word vba and found that some styles would not change back to Normal I think because the new style I had applied, applied to the whole paragraph, rather then just selected text.

User avatar
SpeakEasy
5StarLounger
Posts: 601
Joined: 27 Jun 2021, 10:46

Re: Remove a character style

Post by SpeakEasy »

Does not something like

If Selection.Style.Type = wdStyleTypeCharacter Then Selection.ClearCharacterStyle

do what you are asking?

(there are other Styles of course, for which there are other Clear<xxxx>Style/Formatting methods available)

User avatar
ErikJan
BronzeLounger
Posts: 1302
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Re: Remove a character style

Post by ErikJan »

p45cal wrote:
11 Jun 2024, 12:03
A guess:

Code: Select all

Selection.Style = ActiveDocument.Styles("Normal")
I opened a new blank Word document, pasted in some plain text then examined what style it was. It was Normal.
I'm not familiar with Word vba and found that some styles would not change back to Normal I think because the new style I had applied, applied to the whole paragraph, rather then just selected text.
That is how (some?) styles work, yes.

Ctrl-Spacebar resets the style

User avatar
ErikJan
BronzeLounger
Posts: 1302
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Re: Remove a character style

Post by ErikJan »

I'm playing a bit and found that in the GUI, when using the Style Inspector, one can remove the Character styles for selected text.
Assume that's the same as the above "Selection.ClearCharacterStyle".
"selection.Style.namelocal" gives me the name of the style use for the selected text.

Not sure how that helps me... but I'll trying things

User avatar
ErikJan
BronzeLounger
Posts: 1302
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Re: Remove a character style

Post by ErikJan »

If I select text and apply a Paragraph-style that sets the text to be bold and then a Character-style that sets e.g. Italics then selecting the text and clicking Ctrl-Space seems to remove the Character-style but leave the Paragraph-style alone... Still need to do more tests but was looking to see if someone can confirm as I don't seem to be able to find that described (all I find is that Ctrl-Space should clear all formatting)

User avatar
HansV
Administrator
Posts: 79045
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Remove a character style

Post by HansV »

Yes - Ctrl+Spacebar resets character formatting to that of the paragraph style. It does not remove the character formatting of the paragraph style.
(You can reset the paragraph formatting, such as alignment and margins, to that of the paragraph style by pressing Ctrl+Q)
Best wishes,
Hans

User avatar
ErikJan
BronzeLounger
Posts: 1302
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Re: Remove a character style

Post by ErikJan »

Great. So if I think out loud what I want could indeed work...

I have a document where, for certain pieces of text through the document, I apply one specific character style and for the rest paragraph styles. There are no other character styles being used in the document.
Then I can remove the character style (if present) in any piece of selected text if I just do Ctrl-Spacebar (or the VBA equivalent).
Correct? (I'll test this of course)

Update: That's somewhat of a problem as e.g. sub- and superscripts and URLs formatted as links will also reset... hmmm...

User avatar
HansV
Administrator
Posts: 79045
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Remove a character style

Post by HansV »

Ctrl+Spacebar will reset ALL character formatting to that of the paragraph style. The same holds for Selection.Font.Reset in VBA.

But Selection.ClearCharacterStyle will remove all character styles, but leave directly applied formatting alone. So if you applied a character style that applies superscript, the superscript will be removed. But if you applied superscript directly, it will survive Selection.ClearCharacterStyle.
Best wishes,
Hans

User avatar
ErikJan
BronzeLounger
Posts: 1302
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Re: Remove a character style

Post by ErikJan »

That's great Hans... Another step forward! Regretfully the HyperLink style doesn't survive as that is a character style as well. The rest seems all OK now.