Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-14-2016, 02:16 PM
jroger05 jroger05 is offline How to add a hyperlink in VBA to a bookmark within a document Windows XP How to add a hyperlink in VBA to a bookmark within a document Office 2003
Novice
How to add a hyperlink in VBA to a bookmark within a document
 
Join Date: Aug 2010
Posts: 6
jroger05 is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 07-14-2016, 07:16 PM
DougMVP DougMVP is offline How to add a hyperlink in VBA to a bookmark within a document Windows 7 32bit How to add a hyperlink in VBA to a bookmark within a document Office 2010 32bit
Advanced Beginner
 
Join Date: Nov 2013
Posts: 50
DougMVP will become famous soon enough
Default

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"
Reply With Quote
  #3  
Old 07-18-2016, 04:38 AM
jroger05 jroger05 is offline How to add a hyperlink in VBA to a bookmark within a document Windows XP How to add a hyperlink in VBA to a bookmark within a document Office 2003
Novice
How to add a hyperlink in VBA to a bookmark within a document
 
Join Date: Aug 2010
Posts: 6
jroger05 is on a distinguished road
Default Not sure how to integrate code into mine.

Doug,

To describe this a little better. I have a table with 4 columns.
0x0
R
Red_Version_ID_Reg
FPGA Version ID register
0x2
RW
Red_InfoSec_Status_Reg
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
Reply With Quote
  #4  
Old 07-20-2016, 02:17 AM
DougMVP DougMVP is offline How to add a hyperlink in VBA to a bookmark within a document Windows 7 32bit How to add a hyperlink in VBA to a bookmark within a document Office 2010 32bit
Advanced Beginner
 
Join Date: Nov 2013
Posts: 50
DougMVP will become famous soon enough
Default

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
Reply With Quote
  #5  
Old 07-20-2016, 06:37 AM
jroger05 jroger05 is offline How to add a hyperlink in VBA to a bookmark within a document Windows XP How to add a hyperlink in VBA to a bookmark within a document Office 2003
Novice
How to add a hyperlink in VBA to a bookmark within a document
 
Join Date: Aug 2010
Posts: 6
jroger05 is on a distinguished road
Default

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))
Reply With Quote
Reply

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
How to add a hyperlink in VBA to a bookmark within a document 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

Other Forums: Access Forums

All times are GMT -7. The time now is 07:12 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft