Results 1 to 5 of 5
  1. #1
    Lounger
    Join Date
    Aug 2001
    Location
    Perth,(aka middle of nowhere), Western Australia, Australia
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Delete Hyperlinks (VBA W97 SR2)

    I am trying to remove a large number of hyperlinks from a Word 97 doc.

    Using the following code based on a post related to Powerpoint the goal is partly achieved but the problem is that it skips and deletes the 1st, 3rd, 5th seventh etc hyperlink. I suspect its because the hyperlink index is reassigned and the "Next hyplink" skips it forwards but am not sure how to change it so it retains context - any suggestions? <img src=/S/scratch.gif border=0 alt=scratch width=25 height=29>

    ...
    With ActiveDocument
    If .Hyperlinks.Count > 0 Then
    For Each hypLink In Documents(1).Hyperlinks
    hypLink.Delete
    Next hypLink
    End If
    End With

  2. #2
    Lounger
    Join Date
    Aug 2001
    Location
    Perth,(aka middle of nowhere), Western Australia, Australia
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Delete Hyperlinks (VBA W97 SR2)

    OK, don't know if this is necessarily the most elegant way but the following works because it starts from the bottom link and works its way backwards,


    Dim Ctr As Integer
    If MsgBox("Remove hyperlinks from the active doc?", vbQuestion + vbYesNo) _
    <> vbYes Then Exit Sub

    With ActiveDocument
    Ctr = .Hyperlinks.Count
    If Ctr > 0 Then
    Do
    Documents(1).Hyperlinks(Ctr).Delete
    Ctr = Ctr - 1
    Loop Until Ctr = 0
    End If
    End With

  3. #3
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Delete Hyperlinks (VBA W97 SR2)

    Hi,

    Looping backwards is the way to go in this case.
    Another way to do it is:

    For i = ActiveDocument.Hyperlinks.Count To 1 Step - 1
    ActiveDocument.Hyperlinks(i).Delete
    Next i

    Gary

  4. #4
    Lounger
    Join Date
    Aug 2001
    Location
    Perth,(aka middle of nowhere), Western Australia, Australia
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Delete Hyperlinks (VBA W97 SR2)

    Thanks Gary, much cleaner.
    This code reminds me of my old PDP, VAX (the computer not the vacuum cleaner, and DEC10 days - thats pre-Internet for those of you reading this and asking "What's a PDP?". <img src=/S/gramps.gif border=0 alt=gramps width=20 height=20>

    I've become accustomed to splitting logic into several lines for some reason rather than the old fashioned (and much simpler) method you use. Time to re-visit my roots!

    Not sure if my original problem would also occur in the solution that Jefferson proposed in Post: 71497.
    Thanks again

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Delete Hyperlinks (VBA W97 SR2)

    I don't know why the For Each approach worked in PPT and not WRD, but there are so many other differences that I would never expect consistent behavior in Office.

Posting Permissions

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