Consulting

Results 1 to 11 of 11

Thread: Solved: Select text from table and link to bookmark

  1. #1

    Solved: Select text from table and link to bookmark

    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:

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

    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:

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

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

    Thanks a lot!!!!

    misticmoon

  2. #2
    Site Admin
    The Princess
    VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Welcome, mistic! I added "VBA tags" to your post. See my signature.

    Good luck!
    ~Anne Troy

  3. #3
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    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:

    [vba]sCellContent = TrimCellText(rng.Text)[/vba]
    ~Anne Troy

  4. #4
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    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.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  5. #5
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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 ..
    [VBA]
    ActiveDocument.Tables(1).Cell(2, 8).Range.Select
    Selection.MoveEnd , -1
    ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, _
    SubAddress:="Bookmark_1", TextToDisplay:=Selection.Range[/VBA]

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

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  6. #6
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Hi Anne,

    What is TrimCellText?

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

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  7. #7
    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,

    [VBA]

    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
    [/VBA]

    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

  8. #8
    By the way, I am using Word XP.

    misticmoon

  9. #9
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi misticmoon,
    The following (simplified) version seems to work.
    [VBA] 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

    [/VBA]

  10. #10
    Thanks to all!!

    Mdmackillop solution works fine.

    Thanks again.

  11. #11
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    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!
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •