Removing ALL hyperlinks

B

Bugsy

I have copied a document from the internet into a WORD document (WORD 2002).
The copied document has hundreds of "hyperlinks" and "Permalinks" in it.

How can I remove ALL the links at one time (from the entire copied
document)? It would take forever to eliminate each one separately, I think.

I am using Windows XP Home edition.

Thanks for any suggestions.
 
J

Jonathan West

Bugsy said:
I have copied a document from the internet into a WORD document (WORD
2002). The copied document has hundreds of "hyperlinks" and "Permalinks" in
it.

How can I remove ALL the links at one time (from the entire copied
document)? It would take forever to eliminate each one separately, I
think.

I am using Windows XP Home edition.

Thanks for any suggestions.

Dim n As Long
For n = 1 to ActiveDocument.Range.Hyperlinks.Count
Activedocument.Range.Hyperlinks(1).Delete
Next n
 
E

Edward Thrashcort

Dim n As Long
For n = 1 to ActiveDocument.Range.Hyperlinks.Count
Activedocument.Range.Hyperlinks(1).Delete
Next n

Should that be

For n = ActiveDocument.Range.Hyperlinks.Count Step -1
Activedocument.Range.Hyperlinks(n).Delete
Next n


Eddie
 
D

Doug Robbins - Word MVP

No, it will work.

The following however

Dim n As Long
For n = 1 to ActiveDocument.Range.Hyperlinks.Count
Activedocument.Range.Hyperlinks(n).Delete
Next n

would delete the first, then what was originally the third and then what was
originally the seventh, etc until it errors out because n becomes greater
than the number of paragraphs remaining in the document.

That said however, I usually start from the .Count and step backwards
through the collection when deleting items.


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
E

Edward Thrashcort

Doing it your way it seems that you have to wait for the collection to
"shuffle down" before removing the next link.
For n = ActiveDocument.Range.Hyperlinks.Count Step -1

Should have been

For n = ActiveDocument.Range.Hyperlinks.Count ...TO 1... Step -1

Eddie
 
J

Jonathan West

Edward Thrashcort said:
Should that be

For n = ActiveDocument.Range.Hyperlinks.Count Step -1
Activedocument.Range.Hyperlinks(n).Delete
Next n

Try it the various different ways. They both work, but my way is marginally
the faster. The reason is that while the collection of hyperlinks is
presented as a collection, its underlying implementation is not a linked
list, and it therefore takes more time to get at the last hyperlink in the
set than the first.

Each time you remove the first hyperlink, the next one becomes the first,
and therefore the easiest to get at. You repeat that process as many times
as there were hyperlinks at the start.

Believe me, I have studied and tested this quite extensively. The code looks
a bit counterintuitive, but once you understand the underlying issues, it
does become clear.
 
H

Helmut Weber

Hi Jonathan,
Each time you remove the first hyperlink, the next one becomes the first,
and therefore the easiest to get at. You repeat that process as many times
as there were hyperlinks at the start.

sure.

Of course, the question was about the active document,
where your approach works perfectly and fast.

But there is a strange case,
where deleting hyperlinks(1) doesn't work.

It is if one wants to delete all hyperlinks in the selection
and the selection starts with a hyperlink
and there is more than one hyperlink in the selection.

In that case, the selection collapses immediately
after deleting the first hyperlink to it's start,
the start of the selection,
and then there is no more hyperlink in the selection to be found.

This is in no way meant as criticism.
It is just a marginal footnote,
a case, where Christian Freßdorf, German MVP,
and me had to think quite a while
about what was going on.

Have a nice day.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Bugsy

Hey, Guys:

All of you sound like computer geniuses to me.

Unfortunately, I am a novice. That's why I asked for help.

Undoubtedly, in my mind, what you have suggested will work . . . but I still
don't understand how to implement your suggestions.

Could you advise me on a more elementary level? Thanks!
 
J

Jonathan West

Bugsy said:
Hey, Guys:

All of you sound like computer geniuses to me.

Unfortunately, I am a novice. That's why I asked for help.

Undoubtedly, in my mind, what you have suggested will work . . . but I
still don't understand how to implement your suggestions.

Could you advise me on a more elementary level? Thanks!

Take a look here

What do I do with macros sent to me by other newsgroup readers to help me
out?
http://www.word.mvps.org/FAQs/MacrosVBA/CreateAMacro.htm
 
R

Russ

Great information gentlemen.

Jonathan,
How could we tell what objects work like hyperlinks and are best removed by
removing the first one?
 
J

Jonathan West

Russ said:
Great information gentlemen.

Jonathan,
How could we tell what objects work like hyperlinks and are best removed
by
removing the first one?

Basically, by experiment. A rule of thumb is that if the items of a
collection cannot be indexed by a name, but only by an index number, then
the chances are that they are best removed starting at the front. But its
best to check by benchmarking.
 
E

Edward Thrashcort

I noticed back in Word2k times that the Selection object developed a lot of
quirks. I usually assign a Selection to a Range object before working on
it. Seems less quirky that way.

Eddie
 
E

Edward Thrashcort

Interesting! I'll remember that trick.

Eddie
*From:* "Jonathan West" <[email protected]>
*Date:* Sat, 4 Aug 2007 13:57:52 +0100




Try it the various different ways. They both work, but my way is
marginally the faster. The reason is that while the collection of
hyperlinks is presented as a collection, its underlying implementation is
not a linked list, and it therefore takes more time to get at the last
hyperlink in the set than the first.

Each time you remove the first hyperlink, the next one becomes the first,
and therefore the easiest to get at. You repeat that process as many
times as there were hyperlinks at the start.

Believe me, I have studied and tested this quite extensively. The code
looks a bit counterintuitive, but once you understand the underlying
issues, it does become clear.

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
 

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