Remove a character style

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

Re: Remove a character style

Post by SpeakEasy »

ErikJan wrote:
17 Dec 2024, 11:58
wombat? ;-)
oops - well, the explanation is that I have a number of 'watermarks' that I often use in code. Originally it was to wateermark code that I was sharing online. But I don't really do that anymore , but the habit was hard to break! Wombat was one of those watermarks.

And I meant to strip it out before posting, which I have now done to the original code above (so this post may not make sense to latecomers to the thread ...)

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

Re: Remove a character style

Post by ErikJan »

Thanks for your code... That removes all character-styles, except Hyperlinks, certainly useful. And that is an answer to my original question.
In the larger problem (in another thread here some time ago), the solution that worked best for me, requires me to remove all character styles. But of course, that should not impact how my document looks. So e.g. the "Strong" character style is removed. But if I manually make characters or words bold, that remains (So I can have bold text without using the "Strong" character style).
For Hyperlinks the only way seems to also manually mimic the style (by setting an underline and color).
I'm looking into automating the process: e.g. Find text with "Strong style", remove that style and then set the text to "Bold" font
(Apologies if it seems weird, but it makes perfect sense for my purpose ;-))

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

Re: Remove a character style

Post by ErikJan »

SpeakEasy wrote:
17 Dec 2024, 12:15
ErikJan wrote:
17 Dec 2024, 11:58
wombat? ;-)
oops - well, the explanation is that I have a number of 'watermarks' that I often use in code. Originally it was to wateermark code that I was sharing online. But I don't really do that anymore , but the habit was hard to break! Wombat was one of those watermarks.

And I meant to strip it out before posting, which I have now done to the original code above (so this post may not make sense to latecomers to the thread ...)
Why do you use: Set oldselection = Selection.Range.Duplicate
and not just: Set oldselection = Selection.Range ?

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

Re: Remove a character style

Post by SpeakEasy »

Because in a slightly earlier iteration of this code involved alteration of the range represented by oldselection which alters Selection. Range as well if we don'rt use Duplicate. It doesn't matter so much in the posted version. Fell free to remove .Duplicate if you like.

snb
5StarLounger
Posts: 678
Joined: 14 Nov 2012, 16:06

Re: Remove a character style

Post by snb »

If you want to replace the style of characters that have a custom style you might use:

Code: Select all

Sub M_snb()
    For Each it In ThisDocument.Characters
      If it.Style = "snb" Then it.Bold = True
    Next
End Sub

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

Re: Remove a character style

Post by ErikJan »

Thanks snb, I was working on some code to e.g. replace the "Strong" style by Bold. My code is similar to what you suggest

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

Re: Remove a character style

Post by ErikJan »

UPDATE -- Maybe I was too fast posting - I think there's still a small error - sorry

This replaces the three character styles "Bold", "Emphasis" and "Hyperlink" with directly applied formatting in the selected text.
(I could add ScreenUpdating = false to speed up and I might lose the test to see if the Style is a character-style)

Code: Select all

Sub Replace_Styles()
    Dim MySelection As Range, MyChar As Range
    Set MySelection = Selection.Range
    '
    For Each MyChar In MySelection.Characters
        MyChar.Select
        With Selection
            If .Style.Type = wdStyleTypeCharacter Then
                Select Case .Style
                    Case "Strong"
                        .ClearCharacterStyle
                        .Font.Bold = True
                    Case "Emphasis"
                        .ClearCharacterStyle
                        .Font.Italic = True
                    Case "Hyperlink"
                        .ClearCharacterStyle
                        .Font.Underline = wdUnderlineSingle
                        .Font.Color = &HC07000
                End Select
            End If
        End With
    Next
    '
    MySelection.Select
End Sub

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

Re: Remove a character style

Post by ErikJan »

There's a flaw in my code and I can't completely fix it. It's going wrong with hyperlinks.
Seems that while the code runs over each character, it sees the while URL (in a hyperlink) also as a character???
So I added that ".Characters.Count" should be one, that indeed skips the whole URL, but the next character is then the 2nd character in the URL (so the first one is skipped).
Seems that I need a different method to traverse through all characters.

snb
5StarLounger
Posts: 678
Joined: 14 Nov 2012, 16:06

Re: Remove a character style

Post by snb »

MYChar is an object by default.
Avoid any 'select' in this case MyChar.select

myselection is redundant.

Code: Select all

Sub Replace_Styles()
    For Each MyChar In selection.Characters
      If MyChar.Style.Type = 2 Then
        Select Case Mychar.Style
        Case "Strong"
- - - -
To avoid hyperlnks:

Code: Select all

Sub M_snb()
    For Each it In ThisDocument.Characters
      If it.Style = "snb Char" And it.Hyperlinks.Count = 0 Then it.Bold = True
    Next
    ThisDocument.Styles("snb Char").Delete
End Sub

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

Re: Remove a character style

Post by ErikJan »

Thanks, but if I try that, I get an error on ".ClearCharacterStyle"

snb
5StarLounger
Posts: 678
Joined: 14 Nov 2012, 16:06

Re: Remove a character style

Post by snb »

I woould not use .clearcharacterstyle : see my second suggestion.

A sample file would be very helpfuls to illustrate your question, the hiccups of any code and to illustrate the desired results.

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

Re: Remove a character style

Post by ErikJan »

OK, you mean first find all locations and force formatting and when all is done, delete the style from the document?

snb
5StarLounger
Posts: 678
Joined: 14 Nov 2012, 16:06

Re: Remove a character style

Post by snb »

Yes.

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

Re: Remove a character style

Post by ErikJan »

OK, but the flaw I mentioned above (my inability to convert characters in a hyperlink) won't be solved by that.
I'll try to extract something that I can attach here so you can see my problem.

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

Re: Remove a character style

Post by ErikJan »

Hope I can attach a small DOCM file...
You do not have the required permissions to view the files attached to this post.

snb
5StarLounger
Posts: 678
Joined: 14 Nov 2012, 16:06

Re: Remove a character style

Post by snb »

You can't remove a builtin style.
A hyperlink is not only a question of 'formatting'/'style'.

Code: Select all

For Each it In ThisDocument.Hyperlinks
      it.Range.Style = "Normal"
      it.Range.Font.Underline = 1
      it.Range.Font.Color = vbRed
      it.Delete
   Next

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

Re: Remove a character style

Post by ErikJan »

Thanks. I'm not trying to remove a built-in style, I'm making sure it's not used in my document. As I explained earlier, I can't have character-styles in my document. So my only option is to replace these with directly applied formatting so visually they look the same. For Bold and Italics, I have things covered. For Hyperlinks I have difficulties applying direct formatting before I remove that Style from the selected text.
I don't want to remove the hyperlink itself (or the way it works), but only the Hyperlink Style that formats it to be visible.

(From my perspective, my initial case, question and goals haven't changed. Still, I seem -again- to have difficulties explaining what I want here. I'm sorry for that, but I'm trying and I appreciate the help and patience ;-))

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

Re: Remove a character style

Post by SpeakEasy »

Still slow for large selection ranges, but maybe this is closer to what you want:

Code: Select all

Sub ClearCharacterStyle2()
    Dim myfont As Font
    Dim mychar As Range
    
    For Each mychar In Selection.Characters
        If mychar.Style.Type = wdStyleTypeCharacter Then
            Set myfont = mychar.Font.Duplicate
            mychar.Font.Reset
            mychar.Font = myfont
        End If
    Next
End Sub
Last edited by SpeakEasy on 23 Dec 2024, 15:17, edited 1 time in total.

snb
5StarLounger
Posts: 678
Joined: 14 Nov 2012, 16:06

Re: Remove a character style

Post by snb »

Did you consider ?

Code: Select all

Sub M_snb()
    With ActiveDocument.Styles("Hyperlink").Font
        .Italic = True
        .Underline = 1
        .Color = 255
    End With
End Sub

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

Re: Remove a character style

Post by ErikJan »

SpeakEasy wrote:
19 Dec 2024, 00:48
Still slow for large selection ranges, but maybe this is closer to what you want:

Code: Select all

SSub ClearCharacterStyle2()
    Dim myfont As Font
    Dim mychar As Range
    
    For Each mychar In Selection.Characters
        If mychar.Style.Type = wdStyleTypeCharacter Then
            Set myfont = mychar.Font.Duplicate
            mychar.Font.Reset
            mychar.Font = myfont
        End If
    Next
End Sub
With respect but I have no idea what this does and how it could help met with my problem with the Hyperlink style