Why does adding a hyperlink crash Word?

E

Ed from AZ

In Word 2007, I'm trying to add a hyperlink, but it keeps crashing
Word. What I'm trying to accomplish is to iterate through my
Favorites folder and write these as hyperlinks into a Word document.
I found some code on the MSDN Library site that uses the FSO to
recursively iterate through a folder and write the file path to
everything into an FSO Dictionary, then reads it all out.

When it read out with Debug.Print, all worked fine. Then I added a
few things to write these into the doc as hyperlinks, and it crashes
every time. Here's the crash point:

strLink = varItem
Set rng = doc.Paragraphs(doc.Paragraphs.Count).Range
doc.Hyperlinks.Add Anchor:=rng, _
Address:=strLink, TextToDisplay:=strName
doc.Content.InsertAfter vbCrLf

varItem is from
For Each varItem In dctDict
where dctDict is the FSO dictionary.

When the crash occurs ("We're sorry but Word has encountered a problem
and must shut down. Would you like to recover this and send an error
report?"), the Hyperlinks.Add line is in yellow. When I open the
recovered document, the hyperlink is indeed in the document, with the
correct text displayed and the correct address; the vbCrLf is not
added.

Can anyone shed any light here, please?

Ed
 
E

Ed from AZ

Okay - I'm kicking myself!! Hyperlink.Add doesn't take strings and
ranges, but variants and an object. With that corrected, the code
does not crash. (Still doesn't quite do what I want, but it's a
start!!)

Ed
 

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