Remove a character style

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

Re: Remove a character style

Post by ErikJan »

snb wrote:
19 Dec 2024, 10:36
Did you consider ?

Code: Select all

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

As I said... I want to remove the Hyperlink.Style (but not the hyperlink itself) from my document. Your code here simply changes the formatting of the style...

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

Re: Remove a character style

Post by ErikJan »

ErikJan wrote:
18 Dec 2024, 17:26
Hope I can attach a small DOCM file...
So I extracted and shared this code in the hope that it would be clear where the problem is. I think that this is the best way to let others see where my problem lies.
Even if my goal is still unclear somehow (despite my attempts to explain it), the fact that the code I shared behaves extremely weird and illogical (to me at least - just step over it with F8 and see what happens to the document!) should trigger someone.
Somewhat to my surprise, no one seems to have looked at that. And yes: if I could make that code work, it would solve my problem (it works for Strond > Bold and Emphasis > Italics without problems)

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

Re: Remove a character style

Post by SpeakEasy »

>With respect but I have no idea what this does and how it could help met with my problem with the Hyperlink style

With respect, did you try it? It addresses "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", and provides more consistent results in your test document. Which, yes, I had looked at.

(note that it won't completely fix issues with the hyperlink style, since the hyperlink style behaves ... oddly)

But I think I am done here. Mainly because messing with styles like this is superficially easy, but fraught with unexpected repercussions ... for example try inserting Debug.Print .Text after With Selection in your Test Dummy code ...

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

Re: Remove a character style

Post by snb »

Why would you want to remove the Hyperlink style ??
Does it harm ? Does it obfuscate things ? Does it hamper any proceedings ?
It is very unclear what you are after and what you expect to gain.

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:
23 Dec 2024, 16:35
>With respect but I have no idea what this does and how it could help met with my problem with the Hyperlink style

With respect, did you try it? It addresses "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", and provides more consistent results in your test document. Which, yes, I had looked at.

(note that it won't completely fix issues with the hyperlink style, since the hyperlink style behaves ... oddly)

But I think I am done here. Mainly because messing with styles like this is superficially easy, but fraught with unexpected repercussions ... for example try inserting Debug.Print .Text after With Selection in your Test Dummy code ...
I know it's weird and I indicated that earlier:
"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 whole 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.
"

The suggestion after that was that I should share some code that demonstrates that, which is what I did...

Your code finds all Character styles, get the Font, reset the Font and then re-applies the Font. I miss how that helps me with the specific hyperlink problem I'm facing. But you are right of course, as I found out as well, all goes well -until now- with my goal, expect for Hyperlinks which behave very strangely.

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

Re: Remove a character style

Post by ErikJan »

snb wrote:
23 Dec 2024, 17:06
Why would you want to remove the Hyperlink style ??
Does it harm ? Does it obfuscate things ? Does it hamper any proceedings ?
It is very unclear what you are after and what you expect to gain.
OK, let me try to describe why I want this (most of this discussion took place in another thread here).
  • I'm writing a document which is intended for two audiences, let's say a manual for a car
  • There's many chapters which describe parts of the car
  • Consumers / end-users only need to know and see the information intended for them
  • Technicians, require additional paragraphs with more in depth information (these sections should not be visible for end-users!)
As there's many elements to describe, I want to create ONE document and then add some VBA code which allows me to switch between versions quickly. Of course I don't want to duplicate any information.
What came out as the best solution for me in the other thread is to apply a character-style to the "Technician-paragraphs". Then my VBA code would edit that style and toggle the font to "Hidden" and back, depending on the version required.
Apart from some issues with headers (which I solved as well), this works great. After each toggle my VBA code rebuilds figure #s, table #s and the ToC and all is ready.
The only problem is that by applying the special character style, all other character styles are overwritten. So the only way this works is if I don't have any other character styles in my document (but as I want the formatting still to be visible, my code replaces these styles with manual formatting).
Again, everything works flawlessly now, except the removal of the hyperlink character style.

Hope this explains why I want this. I know that there might be other way to do this (see the other thread) but this one works great for me and is almost done (so I'm sticking to it) ;-)

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

Re: Remove a character style

Post by ErikJan »

Code: Select all

    For Each Link In MySelection.Hyperlinks
        With Link.Range
            .Underline = wdUnderlineSingle
            .Font.Color = wdColorRed
        End With
    Next
It occurred to me that there's a Hyperlink and a Hyperlink-Style. I want to leave the first one alone (and manually format that) and remove the second one. So I focused on the former first and found the code above (which works).
Now all that is left is to remove the Hyperlink-Style from my selection?

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

Re: Remove a character style

Post by SpeakEasy »

>get the Font, reset the Font and then re-applies the Font

Not quite. It takes a copy of the font is currently in use that has been applied by the style (which includes all the various font settings such as bold, italic, ligatures etc), then removes the style (.Reset removes handily removes character styles as Hans advised much earlier in this thread), and then reapplies the font that they style had applied but WITHOUT the style being assigned anymore (it also maintains any directly applied user formatting). The overall effect is that character styles are removed (but not paragraph styles), yet the appearance remains unchanged. Which is what you asked for, and it works fine for removing the hyperlink style;

The oddities one might se when messing about with the hyperlink style is that the way a hyperlink is applied is to sandwich character formatting between two paragraph formatting letters (the first and last letter) , so code that works on a character by character basis misses those two letters out, which may appear odd if you have decided to not only strip the style but change the colour while maintaining hyperlink behaviour (and even if you have not changed the colour will quickly become apparent if you visit the link, as only the first and last characters will correctly reflect the system's visited hyperlink colour)

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, that seems to make sense. I tried that but -as you indicate- also your code leaves the first and last characters of the hyperlink-style unchanged.

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

Re: Remove a character style

Post by ErikJan »

I think I got it finally! :-)

Code: Select all

Sub Replace_Hyperlink()
    Dim MySelection As Range, Link As Hyperlink
    Set MySelection = Selection.Range
    'Make sure normal view is enabled
    ActiveWindow.View.ShowFieldCodes = False
    '
    For Each Link In MySelection.Hyperlinks
        With Link.Range
            .Underline = wdUnderlineSingle
            .Font.Color = wdColorRed
        End With
        Link.Range.Text = Link.Range 'Removes Hyperlink
    Next
    '
    MySelection.Select
End Sub

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

Re: Remove a character style

Post by SpeakEasy »

>'Removes Hyperlink

And removes it from the hyperlinks collection - so, if your selection has more than 1 hyperlink in it then they will not all be modified by this code ... you need to start at the end of the collection and work towards the beginning.

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

Re: Remove a character style

Post by ErikJan »

Yep, true indeed. I hadn't really tested this, but I was so happy that posted it immediately ;-).
I'll update the code (and do some more testing).
Thanks for your help and patience (and also thanks to all others you added suggestions in this 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 »

Code: Select all

Sub Replace_Hyperlink()
    Dim MySelection As Range, CntLinks As Long, i As Long
    'Make sure normal view is enabled
    ActiveWindow.View.ShowFieldCodes = False
    '
    Set MySelection = Selection.Range
    CntLinks = MySelection.Hyperlinks.Count
    '
    For i = CntLinks To 1 Step -1
        With MySelection.Hyperlinks(i)
            .Range.Underline = wdUnderlineSingle
            .Range.Font.Color = wdColorRed
            .Range.Text = .Range 'Removes Hyperlink
        End With
    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 »

Sorry... there's still a bug... now my hyperlink (not the style) doesn't work anymore...

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

Re: Remove a character style

Post by SpeakEasy »

> now my hyperlink (not the style) doesn't work anymore
Well, no - as I said earlier you'll need to live with the inconsistency IF you wan to "maintain[] hyperlink behaviour"

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

Re: Remove a character style

Post by ErikJan »

I'm not saying that you are wrong, but if I create a document manually and add a link, I can e.g. apply the "Normal" style to the link (which makes the Hyperlink Style disappear) and then manually format the text of the link to fully resemble a 'normal' hyperlink (e.g. italics, underlined and blue).
The result looks identical to the original and what's most important: the hyperlink itself also still works.
Point being that manually this can be done easily, all I want is to do it via VBA.
And yes, the only problem is that I don't -always- want to apply the "Normal" style (but probably the style active in the text before and/or after the hyperlink-style maybe), so in reality I want to remove the Hyperlink-style, not overwrite it with another Style (I think).
No problems. I like the challenge. I'll continue searching and testing ;-)

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

Re: Remove a character style

Post by ErikJan »

With the risk that I'm again missing something... I'm posting a new update. First quick testing seems to indicate that this works.
The essence here is to replace the Hyperlink Style with the DefaultParagraphFont style.

(and I added manual formatting to mimic the active Hyperlink style)

Comments welcome.

Code: Select all

Sub Replace_Hyperlinks()
    Dim MySelection As Range, CntLinks As Long, i As Long
    'Make sure normal view is enabled
    ActiveWindow.View.ShowFieldCodes = False
    '
    Set MySelection = Selection.Range
    CntLinks = MySelection.Hyperlinks.Count
    '
    For i = CntLinks To 1 Step -1
        With MySelection.Hyperlinks(i).Range
            .Style = WdBuiltinStyle.wdStyleDefaultParagraphFont 'Removes Hyperlink Style
            'Manually format as Hyperlink
            .Font.Underline = ActiveDocument.Styles("Hyperlink").Font.Underline
            .Font.Color = ActiveDocument.Styles("Hyperlink").Font.Color
            .Font.Italic = ActiveDocument.Styles("Hyperlink").Font.Italic
        End With
    Next
End Sub

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

Re: Remove a character style

Post by SpeakEasy »

It doesn't behave as a hyperlink should do. Sure, you can get the URL to open - but the hyperlink does not change to the 'visited' colour. It does, however, resolve the first/last character anomaly ...

And that may be fine for you

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

Re: Remove a character style

Post by ErikJan »

Agreed, indeed the only thing that seems different is that -as you indicate- the 'visited Style' is not applied. And yes, that's perfectly OK for me.