PDA

View Full Version : Solved: Select text from table and link to bookmark



misticmoon
02-24-2005, 09:03 PM
I want to select text from a table (in Word) and link the text to a bookmark in a document. The problem is that I discovered that there is a difference between selecting a cell in a table and selecting the text in the cell. I have been selecting the cell and trying to add the link but I end up corrupting the table. This may be happening because in Word it is imposible to add a link to a cell itself.


I have been using the following code:

ActiveDocument.Table(1).Cell(2,8).Select
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range _
SubAddress:="Bookmark_1", TextToDisplay:="*"


This code corrupts the table and is selecting the cell (which I think is wrong).

I also tried something like but I received an error:

ActiveDocument.Table(1).Cell(2,8).Select
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Text_
SubAddress:="Bookmark_1", TextToDisplay:="*"

Any help would be appreciated. In my workplace nobody seems to know the answer.

Thanks a lot!!!!

misticmoon:whistle:

Anne Troy
02-24-2005, 09:22 PM
Welcome, mistic! I added "VBA tags" to your post. See my signature. :)

Good luck!

Anne Troy
02-24-2005, 09:25 PM
Aha. Maybe you need something like this, which I don't really have the right to give out, but it's just one little line:

sCellContent = TrimCellText(rng.Text)

Ken Puls
02-24-2005, 10:44 PM
Hi Misticmoon & welcome to VBAX!

Just so you know, I've moved your thread to the Word Forum, as it seems to fit a bit better there. ;)

TonyJollans
02-25-2005, 03:55 AM
Hi Misticmoon,

What version of Word are you using?

When I run your code, I get a hyperlink, but probably not quite what you want. When I change your code slightly to ..

ActiveDocument.Tables(1).Cell(2, 8).Range.Select
Selection.MoveEnd , -1
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, _
SubAddress:="Bookmark_1", TextToDisplay:=Selection.Range

It appears to do what I guess you want - working in Word 2003.

TonyJollans
02-25-2005, 04:00 AM
Hi Anne,

What is TrimCellText?

You say you have no right to give it out - is it part of some package?

misticmoon
02-25-2005, 09:58 AM
Well Tony,

For me, it seems to work if you just want to make one link in the table but if you put the statements in a for loop and try to add links to multiple cells is when I get an error. For example,



Option Base 1
Dim temp_array(3) as Integer

temp_array(1) = 1
temp_array(2) = 2
temp_array(3) = 3

For ii = 1 To 3 'Assuming the table has three rows


ActiveDocument.Table(1).Cell(ii,temp_array(ii)).Select 'Assume I want to add
' links to for all rows and columns 1, 2 , 3
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range _
SubAddress:="Bookmark_1", TextToDisplay:="*"

Next ii


With this code I start corrupting the table. I want to do this in order to avoid adding a link to each cell individually. Thanks to everyone for the help.

misticmoon

misticmoon
02-25-2005, 10:00 AM
By the way, I am using Word XP.

misticmoon

mdmackillop
02-25-2005, 11:10 AM
Hi misticmoon,
The following (simplified) version seems to work.
Option Base 1
Sub DoLink()

With ActiveDocument.Tables(1)
For i = 1 To 3
For j = 1 To 3
MyBK = "BK" & i & j
.Cell(i, j).Select
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:="", _
SubAddress:=MyBK, ScreenTip:="", TextToDisplay:=Selection.Text
Next j
Next i
End With
End Sub

misticmoon
02-28-2005, 09:46 AM
Thanks to all!!

Mdmackillop solution works fine.

Thanks again.

Ken Puls
02-28-2005, 10:24 AM
Hi Misticmoon.

I'm glad you got a solution! Did you know that you can mark your own threads solved here? Just see the info in my signature. I'll get this one for you! ;)