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:
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)?
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
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
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.
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.
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.
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
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)
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)
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...
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.
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.