Hyperlinks.Add Method
Returns a Hyperlink object that represents a new hyperlink added to a range, selection, or document.

Syntax

expression.Add(Anchor, Address, SubAddress, ScreenTip, TextToDisplay, Target)

expression   Required. A variable that represents a Hyperlinks collection.

Parameters

NameRequired/OptionalData TypeDescription
AnchorRequiredObjectThe text or graphic that you want turned into a hyperlink.
AddressOptionalVariantThe address for the specified link. The address can be an e-mail address, an Internet address, or a file name. Note that Microsoft Word doesn't check the accuracy of the address.
SubAddressOptionalVariantThe name of a location within the destination file, such as a bookmark, named range, or slide number.
ScreenTipOptionalVariantThe text that appears as a ScreenTip when the mouse pointer is positioned over the specified hyperlink. The default value is "Address".
TextToDisplayOptionalVariantThe display text of the specified hyperlink. The value of this argument replaces the text or graphic specified by Anchor.
TargetOptionalVariantThe name of the frame or window in which you want to load the specified hyperlink.

Return Value
Hyperlink

Example

This example turns the selection into a hyperlink to the Microsoft address on the World Wide Web.

Visual Basic for Applications
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, _
    Address:="http:\\www.microsoft.com"

This example turns the selection into a hyperlink that links to the bookmark named MyBookMark in MyFile.doc.

Visual Basic for Applications
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, _
    Address:="C:\My Documents\MyFile.doc", SubAddress:="MyBookMark"

This example turns the first shape in the selection into a hyperlink.

Visual Basic for Applications
ActiveDocument.Hyperlinks.Add Anchor:=Selection.ShapeRange(1), _
    Address:="http:\\www.microsoft.com"



Tags :


Community Content

Danimal1
There is a bug in when you dynamically add a hyperlink

There is a bug when dynamically creating a SubAddress within a Hyperlink
The commented out line within the Macro below created in Excel 2007 will fail to create a valid hyperlink:
ActiveSheet.Hyperlinks.Add Anchor:=Selection.Range, Address:="", SubAddress:=strSubAddr, TextToDisplay:=strTextToDisp
The workaround is contained in the 2 lines following the commented out line. The SubAddress is hardcoded, then


Sub CreateHyperLink()
'
' CreateHyperLink Macro
' HyperLink
'

'
Dim strRange As String
Dim strSubAddr As String
Dim strTextToDisp As String
Dim num As Integer
Dim lineNumber As String
Sheets("MIPR Tracking").Select
' Add a hyperlink to 150 cells on an excel spreadsheet (each hyperlink will reference a different worksheet)
' The spreadsheet has 150 different sheets named "MIRP 1" thru "MIPR 150"
For num = 1 To 150
' The First Hyperlink will be added to the "A4" Cell
lineNumber = num + 3
strRange = "A" & lineNumber
Range(strRange).Select
strSubAddr = "'MIPR " & num & "'!A1"
strTextToDisp = "MIPR " & num
'The following dynamic strSubAddr causes the link not to work -- The following 2 lines allow for a workaround (hardcoded SubAddress that is then modified)
'ActiveSheet.Hyperlinks.Add Anchor:=Selection.Range, Address:="", SubAddress:=strSubAddr, TextToDisplay:=strTextToDisp
ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:="'MIPR 3'!A1", TextToDisplay:=strTextToDisp
Selection.Hyperlinks(1).SubAddress = strSubAddr
Next
End Sub


Page view tracker