Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss
Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Use VBA to Insert a hyperlink into a table cell

2,060 views
Skip to first unread message

Wade

unread,
May 23, 2007, 2:25:01 PM5/23/07
to
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

unread,
May 23, 2007, 9:00:35 PM5/23/07
to

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.

Wade

unread,
May 23, 2007, 10:52:00 PM5/23/07
to
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
"13". This is being done from Excel 2003 into Word 2003. Here is code
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

unread,
May 24, 2007, 5:20:40 PM5/24/07
to
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.

Wade

unread,
May 30, 2007, 2:19:03 PM5/30/07
to
Jay,

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

0 new messages