Removing all hyperlinks from word doc

J

Jezebel

Do until ActiveDocument.Hyperlinks.count = 0
activedocument.hyperlinks(1).delete
Loop
 
V

Vipul

Not able to run this snippet acn u pls tell me the
complete procedure to run this script
Thanks
vipul
 
D

Doug Robbins - Word MVP

HI Vipul,

See the article "What do I do with macros sent to me by other newsgroup
readers to help me out?

I don't know how to install them and put them to use" at:

http://www.mvps.org/word/FAQs/MacrosVBA/CreateAMacro.htm

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
L

Larry

Jezebel said:
Do until ActiveDocument.Hyperlinks.count = 0
activedocument.hyperlinks(1).delete
Loop

It's interesting and surprising that this code works, because if you
have code that goes forward through the document, like the first macro
below, it only deletes every other hyperlink:

Dim i As Long

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

This flaw makes it necessary to have code that goes backward through the
document, like the one below. Going backwards works. Yet your code
works as well, even though it goes forward through the document.

Dim i As Long

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

Larry
 
L

Larry

On further thought, I see why this is so. Going forward skips every
other hyperlink because after you delete the first hyperlink, the next
one is now number one, but the macro is looking for number two, so it
skips the (originally) second hyperlink and goes on to the third. But
Jezebel's code avoids that problem by simply eliminating the first
hyperlink in the document until all the hyperlinks are gone.

Larry
 
J

Jezebel

For ... each is a general solution to this problem, because it's guaranteed
to iterate every member of the collection, even if the loop itself adds or
removes members:

Dim pHyperlink as Word.Hyperlink
For each pHyperlink in ActiveDocument.Hyperlinks
pHyperlink.delete
Next






Larry said:
It's interesting and surprising that this code works, because if you
have code that goes forward

through the document, like the first macro
 
J

Jonathan West

Jezebel said:
For ... each is a general solution to this problem, because it's guaranteed
to iterate every member of the collection, even if the loop itself adds or
removes members:

Dim pHyperlink as Word.Hyperlink
For each pHyperlink in ActiveDocument.Hyperlinks
pHyperlink.delete
Next

Hi Jezebel,

I suggest you try running that code. You will find that it does in fact only
delete half the links.

Your first answer was better, but there's no need to check the balue of
ActiveDocument.Hyperlinks.Count every time you go round the loop. Therefore,
your first answer can be speeded up a bit by this means

Dim x As Long
With ActiveDocument.Hyperlinks
For x = 1 to ActiveDocument.Hyperlinks.Count
.Item(1).Delete
Next x
End With

With a small number of hyperlinks, this perhaps doesn't make a significant
difference, but if you are deleting thousands of links, avoiding that extra
access of the object model is a noticeable speed improvement.

It might be more expected to use .Item(x).Delete instead of .Item(1).Delete.
The latter is again a bit faster since it is often quicker to access the
first item in a collection than the last in the Word object model.


--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
 
V

Vipul

Thanks a ton It worked
vipul
-----Original Message-----
For ... each is a general solution to this problem, because it's guaranteed
to iterate every member of the collection, even if the loop itself adds or
removes members:

Dim pHyperlink as Word.Hyperlink
For each pHyperlink in ActiveDocument.Hyperlinks
pHyperlink.delete
Next








through the document, like the first macro


.
 
C

Charles Kenyon

If you just want to get rid of the blue underlined text you can change the
Hyperlink character style to be the same as your regular text. The
hyperlinks will still be there and will change the mouse pointer into a
little hand but they won't mess up your printed version.
--

Charles Kenyon

Word New User FAQ & Web Directory:
<URL: http://addbalance.com/word/index.htm>

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide)
<URL: http://addbalance.com/usersguide/index.htm>

See also the MVP FAQ: <URL: http://www.mvps.org/word/> which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
J

Jezebel

Need to do FollowedHyperlink also.


Charles Kenyon said:
If you just want to get rid of the blue underlined text you can change the
Hyperlink character style to be the same as your regular text. The
hyperlinks will still be there and will change the mouse pointer into a
little hand but they won't mess up your printed version.
--

Charles Kenyon

Word New User FAQ & Web Directory:
<URL: http://addbalance.com/word/index.htm>

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide)
<URL: http://addbalance.com/usersguide/index.htm>

See also the MVP FAQ: <URL: http://www.mvps.org/word/> which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 

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