PDA

View Full Version : Why does this ClearHyperlinks sub strip formatting of freshly pasted text?



Helen269
09-13-2022, 06:21 PM
I have this sub below which clears hyperlinks from links. It works, but there's an oddity about it.

If I copy and paste into Word some text that contains hyperlinks and then run this code immediately, it strips the formatting from the links. Blue underlined text becomes black with no underline. Ugly.

But if I paste in the text and save the document, exit Word, and then load the same document back up again, I can run this code and it won't strip the hyperlink formatting. Blue underlined text stays as blue underlined text.

It took me a while to figure out how to stop the formatting stripping, but I'm just wondering... why is it doing that? What's going on?


Sub ClearHyperlinks()
Dim oField As Field
For Each oField In ActiveDocument.Fields
If oField.Type = wdFieldHyperlink Then
oField.Unlink
End If
Next
Set oField = Nothing
End Sub

Helen269
09-14-2022, 03:46 PM
Obviously it's not the VBA so something about the state of a Word doc changes between saving and loading. It's not just saving the doc as it happens even after saving it. No, it has to be opened from a drive for the formatting to stick when you run that sub.

It's not crucial to know, I'm just curious.