Use VBA to Insert a hyperlink into a table cell

W

Wade

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

Jay Freedman

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.

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
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
W

Wade

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

Jay Freedman

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
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 

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