none
VBA code for Hyperlink in WORD

    Question

  • I have a legacy Word template that has a significant amount of VBA code in it. When this code is executed from a PC that has Office 2003 installed it works with no problems. If I execute the same code on a PC that has Office 2007 installed, the Word application crashes.

    This is an example of the code that works in Word 2003 but causes Word 2007 to crash

    Dim ms AS String
    Dim ms2 AS String
    Dim ms3 AS String

    ms = ""
    ms2 = "LinkText"
    ms3 = "Linkext"

    ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:=ms, _
      SubAddress:=ms2, ScreenTip:="", TextToDisplay:=ms3

     

    If I modify the code slightly it will work in Word 2007. The following is the modified code:

    Dim ms AS String
    Dim ms2 AS String
    Dim ms3 AS variant

    ms = ""
    ms2 = "LinkText"
    ms3 = "Linkext"

    ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:=ms, _
      SubAddress:=ms2, ScreenTip:="", TextToDisplay:=ms3

    Note that the only change was dimensioning the ms3 variable to a variant from a String. Is there a bug in the way that Word 2007 now handles the call for adding a hyperlink with the TextToDisplay parameter?

    It is not difficult to correct this in the template, but it now makes me wonder what other changes happened in Word 2007 VBA that I am not aware of hat will cause similar errors. THe hard crash of Word is really a problem.

    Friday, April 01, 2011 8:38 PM

All replies

  • Brilliant solution! I was having the same problem, but the fact that Word kept crashing made it difficult to pinpoint the problem to the TextToDisplay parameter.

    It is strange that trying to enter a simple string for this one parameter causes Word to crash. That's a pretty serious bug that someone at Microsoft should take a look at.

    Monday, May 27, 2013 1:14 PM