Deleting selected hyperlinks

T

tjtjjtjt

I have this code from a previous post by Jay Freedman to delete all the
hyperlinks in a document.
Sub UnlinkHyperlinks()
Dim nHL As Long
For nHL = 1 To ActiveDocument.Hyperlinks.Count
ActiveDocument.Hyperlinks(1).Delete
Next nHL
End Sub

Can this be modified to delete only hyperlinks in the current selection?

Thanks,
 
T

tjtjjtjt

Thanks, Tony, but I tried that. I get Run-time error '5941':
The requested member of the collection does not exist.
 
T

tjtjjtjt

Are you selecting text that contains more than one hyperlink? I would like to
be able to delete several hyperlinks at a time.
 
J

Jay Freedman

It should work no matter how many hyperlinks are in the selected text
(including zero). I tested cases of 0, 1, 2, and 5 hyperlinks in the
selection, plus one or more outside it, without errors.

If you use Ctrl+drag to select two or more disconnected areas, the macro
will delete hyperlinks only in the last area -- that's a restriction on the
way VBA works (or doesn't work) with discontiguous selections. But it still
doesn't cause an error.
 
T

Tony Jollans

Yes, it works for me with any number of hyperlinks (up to 5 anyway for
certain). I just confirmed in both Word 2000 and Word 2003.

If the only fields you have in your selection are hyperlinks you do have
another choice. You can press Ctrl+Shift+F9 or, in code, do

Selection.Fields.Unlink
 
M

Marc Adams [marcmyword.com]

Hi tjtjjtjt :)
If you're using this code exactly as written, you're getting the error
because you are not using the variable in hyperlinks.

Just replace Hyperlinks(1) with Hyperlinks(nHL).

Hope that helps, if not let me know.
 
J

Jonathan West

"Marc Adams [marcmyword.com]"
Hi tjtjjtjt :)
If you're using this code exactly as written, you're getting the error
because you are not using the variable in hyperlinks.

Just replace Hyperlinks(1) with Hyperlinks(nHL).

Hope that helps, if not let me know.


That shouldn't matter, because when you have deleted the first hyperlink,
the next one is the first...

When dealing with large numbers of hyperlinks, deleting the first one
successively is faster.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
J

Jay Freedman

Hi Marc,

That's not the reason for the error (though I'm not sure what is, because I
can't reproduce it). The code as written deletes the first hyperlink in the
Selection during the first pass through the loop. The next time through the
loop, what used to be Hyperlinks(2) is now Hyperlinks(1), so that one gets
deleted. This continues as many times as the original hyperlink count, so
they all go away.

If you try it your way, the first loop deletes the first hyperlink. The
second loop deletes the second one *of the remaining hyperlinks* which
started life as Hyperlinks(3), leaving the original Hyperlinks(2) unchanged.
The third loop deletes the third one of what now remains, which was
originally Hyperlinks(5), leaving behind the old Hyperlinks(4). Eventually
the value of nHL will become larger than the number of remaining hyperlinks,
and then you *will* get the error tj quoted, "The requested member of the
collection does not exist".
 
T

tjtjjtjt

All I can tell you is that I'm getting the error I posted previously.

I'm going to try the modification suggested by Marc. For what it's worth
putting the varialble in the code causes an error when trying to delete all
the hyperlinks in the document.
 
H

Helmut Weber

Hi everybody,

That shouldn't matter,

Still more so, it'll be about the only method,
that doesn't work.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
T

tjtjjtjt

This didn't work for me, either. I'm still getting Run-time error '5941':

Perhaps this has something to do with a Service Pack for Office XP? This is
happening in Word 2002 with SP3. I have Office 2003 with SP1 at home. I'll
try these variations on my home computer when I get there.

Thanks,
 
H

Helmut Weber

I bet,

you overlooked "(in two places)" in Tony's posting.

Otherwise, show us how your code looks right now.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
T

tjtjjtjt

I didn't overlook it in what I've been trying.

Sub UnlinkHyperlinks()
Dim nHL As Long
For nHL = 1 To Selection.Hyperlinks.Count
Selection.Hyperlinks(1).Delete
Next nHL
End Sub
 
G

Greg Maxey

Do it like this:

Sub UnlinkHyperlinks()
Dim nHL As Long
For nHL = Selection.Hyperlinks.Count To 1 Step -1
Selection.Hyperlinks(nHL).Delete
Next nHL
End Sub
 
T

tjtjjtjt

This is failing after unlinking one hyperlink:
Sub UnlinkHyperlinks()
Dim nHL As Long
For nHL = 1 To Selection.Hyperlinks.Count
Selection.Hyperlinks(1).Delete
Next nHL
End Sub

All I did was copy the original code and replace ActiveDocument with
Selection. This is failing in Word 2002 SP3 and Word 2003 SP1.
 
H

Helmut Weber

Hi

that's really interesting.

I tried quite a few different ways, and none of them worked.
Some of the hyperlinks were gone, seems to be only every second,
some of them just changed color without reason.

I think, the selection is changing, when deleting a hyperlink.

How about this one:

Sub test1234567()
Dim l As Long ' hyperlinks counter
Dim r As Range ' just a range
Dim p1 As Long ' start of the range
Dim p2 As Long ' end of the range

Set r = Selection.Range
p1 = r.Start ' remember
p2 = r.End ' remember
l = r.Hyperlinks.Count
While l > 0
r.Hyperlinks(1).Delete
r.Start = p1 ' restore
r.End = p2 ' restore
l = r.Hyperlinks.Count
Wend
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
G

Greg Maxey

tj,

I can't explain why, but it appears that the methord that you where using
results in the selection being collapse to the start after the first
hyperlinks is deleted. I suppose that since there is no hyperlink now in
the selection appearing as the insertion point that the error is generated.
For some reason when you use the method I proposed the selection stays
expanded of the range of the originally selected text. I for one will be
interested in the forthcoming explaination from one of the Senseis.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top