#1
|
|||
|
|||
How to add a hyperlink in VBA to a bookmark within a document
I am trying to use the hyperlinks feature in VBA within word, but when I use the following code it is attempting to create the hyperlink as a link to an existing File or Web page. I want the hyperlink to point to a bookmark named link2create below within the current document. I thought using the address = "" would give me the current document but unfortunately it is not.
ActiveDocument.Hyperlinks.Add Anchor:=oRng, _ Address:="", _ SubAddress:=link2create, ScreenTip:="", _ TextToDisplay:=txt2cpy |
#2
|
|||
|
|||
Use:
Code:
Dim oRng As Range Dim newfield As Field Dim hlink As Hyperlink Set oRng = Selection.Range Set newfield = ActiveDocument.Fields.Add(oRng, wdFieldEmpty, "HYPERLINK \l " & Chr(34) & "Link2Create" & Chr(34)) Set hlink = Selection.Paragraphs(1).Range.Hyperlinks(1) hlink.TextToDisplay = "txt2cpy" |
#3
|
|||
|
|||
Not sure how to integrate code into mine.
Doug,
To describe this a little better. I have a table with 4 columns. 0x0 R FPGA Version ID register 0x2 RW SP to OMAP infosec status register I am traversing the table through the VBA and then adding the hyperlink to the the third column. Obviously my way is unfortunately causing the hyperlink to another document. I am not sure how to integrate your solution into my code. I am sure my code is more complicated than it needs to be, but being a novice it is what I could figure out. Thanks. Sub Create_Memory_Map_HyperLinks() Dim J As Integer Dim iTableNum As Integer Dim oTbl As Table Dim oRow As Row 'Define variables for the processing of cells Dim oCell As Cell Dim sCellText As String 'Define variables for the processing of the cells Dim rw_oCell As Cell Dim rw_sCellText As String ' Define variable for the Reg Name cell Dim oRng As Range ' Define variable for the RW cell Dim RW_oRng As Range ' Define a loop variable to be used for the row being processed Dim tbl_loop_variable As Integer ' If user is currently not in a table then go to the next table If Selection.Information(wdWithInTable) = False Then Selection.GoToNext What:=wdGoToTable End If Selection.Bookmarks.Add ("TempBM") For J = 1 To ActiveDocument.Tables.Count Set oTbl = ActiveDocument.Tables(J) oTbl.Select If Selection.Bookmarks.Exists("TempBM") Then iTableNum = J Exit For End If Next J ActiveDocument.Bookmarks("TempBM").Select ActiveDocument.Bookmarks("TempBM").Delete ' MsgBox "The current table is table " & iTableNum ' Set the current table to be the active table ' ActiveDocument.Tables(iTableNum).Select NumRows = Selection.Tables(1).Rows.Count NumCols = Selection.Tables(1).Columns.Count ' MsgBox "The current table has " & NumRows & " Rows" ' MsgBox "The current table has " & NumCols & " Columns" ' now cycle through the rows of the selected table. tbl_loop_variable = 0 For Each oRow In Selection.Tables(1).Rows ' Need to skip the first two rows of the table ' Select the cell in column 3 tbl_loop_variable = tbl_loop_variable + 1 If tbl_loop_variable > 2 Then ' Handle the Reg Name Set oRng = oRow.Cells(3).Range Set oCell = oRow.Cells(3) sCellText = oCell.Range ' Remove table cell markers from the text. sCellText = Left$(sCellText, Len(sCellText) - 2) ' Handle the R/W Cell Set RW_oRng = oRow.Cells(2).Range Set rw_oCell = oRow.Cells(2) rw_sCellText = rw_oCell.Range ' Remove table cell markers from the text. rw_sCellText = Left$(rw_sCellText, Len(rw_sCellText) - 2) ' MsgBox rw_sCellText ' Check the RW Cell to ensure that it is not blank (ie second line of range) If (rw_sCellText = "R") Or (rw_sCellText = "R/W") Or (rw_sCellText = "W") Or (rw_sCellText = "RW") Then ' MsgBox "In the R/W section" If sCellText = "Reserved" Then ' MsgBox "Found the Reserved" Else If sCellText = " " Then ' MsgBox "Found the Empty Cells" Else ' MsgBox sCellText ' link2create = "C3_" & oRng link2create = oRng ActiveDocument.Hyperlinks.Add Anchor:=oRng, _ Address:="", _ SubAddress:=link2create, ScreenTip:="", _ TextToDisplay:=txt2cpy End If End If End If End If Next oRow End Sub |
#4
|
|||
|
|||
Try
Code:
Sub Create_Memory_Map_HyperLinks() Dim J As Integer Dim iTableNum As Integer Dim oTbl As Table Dim oRow As Row Dim newfield As Field Dim hlink As Hyperlink 'Define variables for the processing of cells Dim oCell As Cell Dim sCellText As String 'Define variables for the processing of the cells Dim rw_oCell As Cell Dim rw_sCellText As String ' Define variable for the Reg Name cell Dim oRng As Range ' Define variable for the RW cell Dim RW_oRng As Range ' Define a loop variable to be used for the row being processed Dim tbl_loop_variable As Integer ' If user is currently not in a table then go to the next table If Selection.Information(wdWithInTable) = False Then Selection.GoToNext What:=wdGoToTable End If Selection.Bookmarks.Add ("TempBM") For J = 1 To ActiveDocument.Tables.count Set oTbl = ActiveDocument.Tables(J) oTbl.Select If Selection.Bookmarks.Exists("TempBM") Then iTableNum = J Exit For End If Next J ActiveDocument.Bookmarks("TempBM").Select ActiveDocument.Bookmarks("TempBM").Delete ' MsgBox "The current table is table " & iTableNum ' Set the current table to be the active table ' ActiveDocument.Tables(iTableNum).Select NumRows = Selection.Tables(1).Rows.count NumCols = Selection.Tables(1).Columns.count ' MsgBox "The current table has " & NumRows & " Rows" ' MsgBox "The current table has " & NumCols & " Columns" ' now cycle through the rows of the selected table. tbl_loop_variable = 0 For Each oRow In Selection.Tables(1).Rows ' Need to skip the first two rows of the table ' Select the cell in column 3 tbl_loop_variable = tbl_loop_variable + 1 If tbl_loop_variable > 2 Then ' Handle the Reg Name Set oRng = oRow.Cells(3).Range Set oCell = oRow.Cells(3) sCellText = oCell.Range ' Remove table cell markers from the text. sCellText = Left$(sCellText, Len(sCellText) - 2) ' Handle the R/W Cell Set RW_oRng = oRow.Cells(2).Range Set rw_oCell = oRow.Cells(2) rw_sCellText = rw_oCell.Range ' Remove table cell markers from the text. rw_sCellText = Left$(rw_sCellText, Len(rw_sCellText) - 2) ' MsgBox rw_sCellText ' Check the RW Cell to ensure that it is not blank (ie second line of range) If (rw_sCellText = "R") Or (rw_sCellText = "R/W") Or (rw_sCellText = "W") Or (rw_sCellText = "RW") Then ' MsgBox "In the R/W section" If sCellText = "Reserved" Then ' MsgBox "Found the Reserved" Else If sCellText = " " Then ' MsgBox "Found the Empty Cells" Else ' MsgBox sCellText ' link2create = "C3_" & oRng link2create = oRng.Text Set newfield = ActiveDocument.Fields.Add(oRng, wdFieldEmpty, "HYPERLINK \l " & Chr(34) & "Link2Create" & Chr(34)) Set hlink = oRng.Paragraphs(1).Range.Hyperlinks(1) hlink.TextToDisplay = "txt2cpy" End If End If End If End If Next oRow End Sub |
#5
|
|||
|
|||
I get an error message:
Run time error 4605 Command not available on the line provided: Set newfield = ActiveDocument.Fields.Add(oRng, wdFieldEmpty, "HYPERLINK \l " & Chr(34) & "Link2Create" & Chr(34)) |
Tags |
hyperlink current |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Correct Bookmark Hyperlink | Phil H | Word | 2 | 02-10-2016 12:55 PM |
Replicating Insert Hyperlink-Bookmark function | Marrick13 | Word VBA | 0 | 02-05-2016 01:27 PM |
Hyperlink: open the document only once, quit & reopen PP, hyperlink doesnt work anymore | quanghuynguyenhua | PowerPoint | 0 | 10-10-2015 06:17 PM |
Clicking a Hyperlink to bookmark in other open doc moves that doc's position! | diracsbracket | Word | 1 | 08-14-2014 04:07 PM |
How to highlight bookmark after clicking hyperlink | readysteadykitty | Word | 0 | 07-04-2013 10:40 PM |