Word - Use VBA to Insert a hyperlink into a table cell

Asked By Wad on 23-May-07 02:25 PM
I am looking for a way to insert a hyperlink into a table cell in Word.  I am
using an excel spreadsheet to create a word doc with a table in it and I
would like to create links as I am filling in the table.  Thanks.


Jay Freedman replied on 23-May-07 09:00 PM
On Wed, 23 May 2007 11:25:01 -0700, Wade


You don't say anything about how you know which cell of which table to
use for the hyperlink, so I can't help you with that until you supply
more information. Once you know where, here's how:

Sub demo()
Dim myRg As Range

' you need to know which cell of which table
Set myRg = ActiveDocument.Tables(1).Cell(2, 2).Range

' read the help topic on the Add method
' of the Hyperlinks collection
ActiveDocument.Hyperlinks.Add Anchor:=myRg, _
Address:="http://www.microsoft.com", _
ScreenTip:="MS website", _
TextToDisplay:="Bill's Place"
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
Wad replied on 23-May-07 10:52 PM
Jay,

Thanks for the response.  I am selecting the cell using a table object I
created:

oTable.Cell(WordRow, Column).Range

I tried the code you sent, but it gave me an error of type mismatch error
snippet of what I am doing now with your code and still getting an error:

'Insert a 11 x 2 table, fill it with data and change the column widths.
Set oTable = oDoc.Tables.Add(oDoc.Bookmarks("\endofdoc").Range, 2, 11)

'Move through all of the columns
For Column = 1 To 11
If Column < 10 Then
oTable.Cell(WordRow, Column).Range.Text =
Sheet1.Cells(ExcelRow, Column)
Else
Dim LinkName As String
Dim Address As String
Dim myRg As Range

' you need to know which cell of which table
Set myRg = oTable.Cell(WordRow, Column).Range

LinkName = Sheet1.Cells(ExcelRow, Column).Hyperlinks(1).Name
Address = Sheet1.Cells(ExcelRow, Column).Hyperlinks(1).Address

ActiveDocument.Hyperlinks.Add Anchor:=myRg, _
Address:=Address, _
ScreenTip:="", _
TextToDisplay:=LinkName
End If
Next


I left out the code to increment the word and excel rows, since I move the
row count up 1 each interation.
Jay Freedman replied on 24-May-07 05:20 PM
Something like that should work, but I see what may be a couple of
problems.

- Because you're executing VBA in Excel to do work in Word, you have
to be very careful about objects that might be ambiguous. In
particular, don't use "ActiveDocument". Instead use oDoc, which
appears to be set to point to the Word document containing the table:

oDoc.Hyperlinks.Add Anchor:=myRg, _
...

Also, in your declarations, it may be necessary to qualify some
objects, such as

Dim myRg As Word.Range

- I think using "Address" as both a variable name and a function
parameter is causing a problem. Change the name of the variable:

Dim LinkAddress As String
...
LinkAddress = Sheet1.Cells(ExcelRow, Column).Hyperlinks(1).Address
...
oDoc.Hyperlinks.Add Anchor:=myRg, _
Address:=LinkAddress, _
...


--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

On Wed, 23 May 2007 19:52:00 -0700, Wade
Wad replied on 30-May-07 02:19 PM
Jay,

Thanks for the reply, I will try to see if it works.