Consulting

Results 1 to 16 of 16

Thread: Remove Select Hyperlinks...

  1. #1
    VBAX Newbie
    Joined
    Mar 2006
    Posts
    4
    Location

    Question Remove Select Hyperlinks...

    I have several Word macros that can and do delete all hyperlinks in any Word document. None provide what I am looking for. I need a Word macro that will delete all external hyperlinks leaving the text in place (but not preserving any underlines or coloring)... I also need to leave in place any hyperlinks that are internal (bookmark) hyperlinks. I want the macro to run without human intervention. No 'yes' or 'no' questions.

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    [vba]Dim oHyperlink As Word.Hyperlink
    For Each oHyperlink In ActiveDocument.Hyperlinks()
    If Left(oHyperlink.Address, 4) = "http" Then
    oHyperlink.Delete
    End If
    Next[/vba]

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    This is trickier than it looks.
    Internal (within the same document) hyperlinks don't seem to have an "address" .
    Within the same folder, within other folders and on other drives hyperlinks appear to have addresses, but without http.
    Web pages have the http. (What about ftp?)
    So I tried the following, which fails dismally, and I don't know why!

    [vba]
    Sub DelH()
    Dim oHyperlink As Word.Hyperlink
    i = 0
    MsgBox ActiveDocument.Hyperlinks.Count
    For Each oHyperlink In ActiveDocument.Hyperlinks()
    i = i + 1
    Debug.Print ActiveDocument.Hyperlinks(i).Address & " - " & _
    ActiveDocument.Hyperlinks(i).TextToDisplay
    If oHyperlink.Address <> "" Then
    oHyperlink.Delete
    End If
    Next
    MsgBox ActiveDocument.Hyperlinks.Count
    End Sub
    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    VBAX Regular
    Joined
    May 2004
    Posts
    12
    Location
    mdmackillop, calling it recursively seems to work. btw I am unable to access the file you attached. Do I need some special rights or something??

    Sub RemoveLink()
    Dim oHyperlink As Word.Hyperlink
    For Each oHyperlink In ActiveDocument.Hyperlinks()
        If oHyperlink.Address <> "" Then
            oHyperlink.Delete: RemoveLink
        End If
    Next
    End Sub

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Rajesh
    Nothing special about the file. Resaved and reposted here. It contains 6 assorted hyperlinks, but the code only deletes 3 of them.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    VBAX Regular
    Joined
    May 2004
    Posts
    12
    Location
    Yeh, if we were to delete the 3rd hyperlink, 4th becomes 3rd and 5th becomes 4th and next time around in the loop, it skips one...

    Sub DelH()
        Dim oHyperlink As Word.Hyperlink
        i = 0
        MsgBox ActiveDocument.Hyperlinks.Count
        For Each oHyperlink In ActiveDocument.Hyperlinks()
        oHyperlink.Range.Select: MsgBox "a"
            i = i + 1
            Debug.Print ActiveDocument.Hyperlinks(i).Address & " - " & _
            ActiveDocument.Hyperlinks(i).TextToDisplay
            If oHyperlink.Address <> "" Then
                oHyperlink.Delete
            End If
        Next
        MsgBox ActiveDocument.Hyperlinks.Count
    End Sub

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Copying and pasting in a few more hyperlinks, it appears to be deleting alternative links only. More are deleted by rerunning the code, until eventually they're gone.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Really, Malcolm!

    Surely you know to delete collection members from the end backwards.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  9. #9
    VBAX Expert
    Joined
    Feb 2005
    Posts
    929
    Location
    Quote Originally Posted by TonyJollans
    Really, Malcolm!

    Surely you know to delete collection members from the end backwards.
    darn and here I thought I could contribute to the Word forum with something useful.
    "It's not just the due date that's important, it's also the do date" [MWE]

    When your problem has been resolved, mark the thread SOLVED by clicking on the Thread Tools dropdown menu at the top of the thread.

  10. #10
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    mdmackillop - Public Profile
    Biography: Still learning after all these years.


    Thanks Tony (and MWE)

    [VBA]
    Sub DelH()
    With ActiveDocument
    For i = .Hyperlinks.Count To 1 Step -1
    If .Hyperlinks(i).Address <> "" Then .Hyperlinks(i).Delete
    Next
    End With
    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  11. #11
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  12. #12
    VBAX Newbie
    Joined
    Mar 2006
    Posts
    4
    Location
    Thanx people... great stuff!

  13. #13
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I not sure I got what happened there. I thought the request WAS to delete external (ie. http) links and leave everything else alone? In which case, the point that internal links do not have an full address (true) is completely irrelevant. Good...then they do not have http, and therefore are not external, and...can be ignored.

    External hyperlinks (http) - delete
    internal (bookmark) hyperlinks (NOT http) - do not delete

    Did I miss something......again?

  14. #14
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Gerry,
    I think "internal" is open to interpretation. Internal to the document, or to files on the PC. I went with the latter, yours deals with the former. I don't know which the OP was after or has used, but I think there are two valid solutions here in either case.
    Regards
    Malcolm
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  15. #15
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Hyperlinks come in many shapes and sizes. The Hyperlinks Collection contains all the HYPERLINK Fields (all external hyperlinks, if you like, not just http and other protocols, but also local files) - it does not contain other Fields which have ("internal") hyperlink capability (REF, PAGEREF, etc.). Deleting all hyperlinks in the Hyperlinks Collection fits the bill as far as I can see.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  16. #16
    VBAX Newbie
    Joined
    Mar 2006
    Posts
    4
    Location

    Smile

    My apologies for being ambiguous... in fact I was wanting to delete all hyperlinks that are not internal to the document. Some stuff I am working with, but not a lot, have TOCs, indexes and appendixes that are linked. It is these that I wish to retain, but delete all others.

    Again, thanx to all...

Posting Permissions

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