Deleting selected hyperlinks

G

Greg Maxey

Helmut,

I believe our posts crossed. The selection collapses to the start after the
first hyperlink is deleted. It doesn't collapse if you count down.
 
T

tjtjjtjt

This one is clearing out all hyperlinks in the document regardless of what is
selected.
 
H

Helmut Weber

Hi Submariner,

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

Sure, Jonathan, Jay, Tony and my humble myself knew about this.

It's an obvious and simple solution.

But as the first obvious and simple methode
Hyperlinks(1).delete
didn't work, got confused and insecure,
and started searching for something quite different.

I still wonder, why Hyperlinks(1).delete doesn't work.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Tony Jollans

It always makes good sense to delete 'bottom up' from collections but no
matter what I try I can't make the original method fail; my selection
doesn't collapse - I guess there's some option which affects it but I don't
know which one.
 
H

Helmut Weber

Hi Greg,

yes,

seems the selection's end collapses to the start
of the deleted hyperlink. So if you start with the
last hyperlink, all preceding hyperlinks are still
in the selection.

Trying, AFAIK, is a recognized method in science...

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

tjtjjtjt

I'm curious as well. Thanks again.
--
tj


Greg Maxey said:
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.


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
 
T

tjtjjtjt

Thank you everybody. I appreciate everyone's input. I have the flexibility I
was looking for now:

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

Tony Jollans

But only if the selection starts at the beginning of a hyperlink. Might it
be that messing with the beginning of the selection forces the collapse?
 
T

tjtjjtjt

Wow, I can get it to succeed too, if the selection doesn't start with a
hyperlink. My initial test was to put 3 paragraphs in a row that only
contained hyperlinks, so it failed immediately for me. Thank goodness for
varied perspectives and the expers on this board.
I'll stick with Greg's code, since it hasn't failed yet with text selected.
 
T

tjtjjtjt

I forgot to mention - it fails when the selection starts with a hyperlink out
of overtype mode.
 
G

Greg Maxey

Tony,

This is really intriguing. I noticed the collapse first thing because I had
selected from the beginning of one hyperlink to the end of a second and then
stepped through the code. The selection collapsed immediately after
deleting the first link. I see now that if I selected just one space and
then the two links the original code worked fine.

It is messing with the beginning of the selection that causes the collapse.
Type a short line of text and consider this test:

Sub Test()
Dim oRng1 As Range
Dim oRng2 As Range
Set oRng1 = ActiveDocument.Range
Set oRng2 = ActiveDocument.Range
oRng1.Start = 4
oRng1.End = 12
oRng1.Select
oRng2.Start = 4 'If 4 or less selection collaspes, if 5 or more it doesn't
oRng2.End = 10
oRng2.Delete


End Sub
 

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