How to make hyperlinks in a word document using access vba on windows server 2008 (2 Viewers)

TDZ7200

New member
Local time
Today, 17:57
Joined
Dec 3, 2012
Messages
7
Dear

I generate a Word-document with information from my Access database. I also add hypertext with hyperlinks to the Word-document.
This works fine on Windows 7 with MS Access2010 using ActiveDocument.Hyperlinks.Add.

Now I run the same code on Windows Server 2008, but I don't see any hyperlinks anymore in my Word-document.
I tried to use Application.FollowHyperlink, but using this command, you are immediately redirected to the file and I need hypertext: the reader of the Word-document must click on the text before he or she is redirected.

Here is some sample code :
Code:
 Option Compare Database
 Public WordApp As Word.Application
Public doc As Word.Document
Public Selection As Word.Selection
  
 Public Sub test()
   Set WordApp = New Word.Application
  WordApp.Documents.Add
  Set doc = WordApp.ActiveDocument
  Set Selection = WordApp.Selection
  WordApp.Visible = True
   
   ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
        "adres", SubAddress:="", ScreenTip:="", TextToDisplay:= _
        "test"
 End Sub

This works on any computer, but on Windows Server 2008, I get the following error :
runtime error '5825'
Object has been deleted

Debugging highlights the ActiveDocument.Hyperlinks.Add

Removing the definition of 'selection' makes the error disappear, but then I don't see any hyperlink appearing in the word document.

Is there a solution ? Is there an alternative to Hyperlinks.Add command ?


Regards
Jurgen
 
Last edited:

sneuberg

AWF VIP
Local time
Today, 09:57
Joined
Oct 17, 2014
Messages
3,506
You could try using the doc object directly as shown below and see if that helps. I don't have Windows Server 2008 so I can't test this.

Code:
Public WordApp As Word.Application
Public doc As Word.Document
Public Selection As Word.Selection

 Public Sub test()
   Set WordApp = New Word.Application
  WordApp.Documents.Add
  Set doc = WordApp.ActiveDocument
  Set Selection = WordApp.Selection
  WordApp.Visible = True

  ' ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
        "adres", SubAddress:="", ScreenTip:="", TextToDisplay:= _
        "test"

    doc.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
        "adres", SubAddress:="", ScreenTip:="", TextToDisplay:= _
        "test"
 End Sub

I'd also check the References to make sure they are right.
 

sneuberg

AWF VIP
Local time
Today, 09:57
Joined
Oct 17, 2014
Messages
3,506
Another thing to try is to prefix ActiveDocument with WordApp like:

Code:
Public WordApp As Word.Application
Public doc As Word.Document
Public Selection As Word.Selection

 Public Sub test()
   Set WordApp = New Word.Application
  WordApp.Documents.Add
  Set doc = WordApp.ActiveDocument
  Set Selection = WordApp.Selection
  WordApp.Visible = True
    
  WordApp.ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
        "adres", SubAddress:="", ScreenTip:="", TextToDisplay:= _
        "test"
 End Sub

With that you could try late binding like:

Code:
Public WordApp As Object
Public doc As Object
Public Selection As Object

 Public Sub test()
   Set WordApp = CreateObject("Word.Application")
  WordApp.Documents.Add
  Set doc = WordApp.ActiveDocument
  Set Selection = WordApp.Selection
  WordApp.Visible = True
    
  WordApp.ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
        "adres", SubAddress:="", ScreenTip:="", TextToDisplay:= _
        "test"
 End Sub
 

TDZ7200

New member
Local time
Today, 17:57
Joined
Dec 3, 2012
Messages
7
Many many thanks, this indeed solved my problem!
(This is really a great forum!)
 

Users who are viewing this thread

Top Bottom