Consulting

Results 1 to 2 of 2

Thread: Why does this ClearHyperlinks sub strip formatting of freshly pasted text?

  1. #1
    VBAX Regular
    Joined
    Jan 2018
    Posts
    34
    Location

    Why does this ClearHyperlinks sub strip formatting of freshly pasted text?

    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
    Last edited by Aussiebear; 09-14-2022 at 01:27 AM. Reason: Added code tags to supplied code

  2. #2
    VBAX Regular
    Joined
    Jan 2018
    Posts
    34
    Location
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •