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