Discussion:
Why does adding a hyperlink crash Word?
(too old to reply)
Ed from AZ
2008-10-22 19:46:06 UTC
Permalink
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
Ed from AZ
2008-10-22 21:52:18 UTC
Permalink
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
Post by 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
        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
Continue reading on narkive:
Loading...