Hyperlinks.Add causes Word 2007 to crash

P

prem1234

Can anyone tell why does Word 2007 crashes when I run this macro?
================================================================

Sub Test1()

Dim wordDoc As Word.Document
Set wordDoc = ActiveDocument

Dim str As String
str = "Hyperlink"
Dim strText As String
strText = "DisplayText"

Dim Hyp As Word.Hyperlinks
Set Hyp = wordDoc.Hyperlinks

Dim sel As Word.Selection
Set sel = Word.Selection

sel.TypeText Text:=" "
Word.ActiveDocument.Hyperlinks.Add Anchor:=sel.Range, Address:= _
str, SubAddress:="", ScreenTip:="", TextToDisplay:=strText
End Sub


==================================================================

If I remove the optional parameter
TextToDisplay:=strText
from the statement

Word.ActiveDocument.Hyperlinks.Add Anchor:=sel.Range, Address:= _
str, SubAddress:="", ScreenTip:="", TextToDisplay:=strText
It works fine!


Thanks in advance,
Prem
 
D

Doug Robbins - Word MVP

Per the Visual Basic Help, str and strtext need to be declared as Variant.
Then it does not cause Word to crash

--
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
 

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