Hyperlink a Feature to a Web Site


The sample demonstrates one way to create a hyperlink between a single feature and a web page. This sample links the ESRI web page (www.esri.com) to the first feature in the focus map's feature selection.

Hotlinks differ from hyperlinks in that they are set up for the entire layer rather than just a single feature. Hotlinks use a field in the database to hold the path to the related document, web site, or macro. See the Hotlink sample for an example of this.

Hyperlinks and hotlinks support three link types: documents, urls, and macros.

How to use:
  1. Paste the code into VBA.
  2. Select a feature.
  3. Run the AddHyperlink Macro.
  4. Select the Hyperlink tool.
  5. The hyperlinked feature should be outline in blue.
  6. Using the Hyperlink tool, click on the feature to launch the web site.
Public Sub AddHyperlink()
  Dim pMxDoc As IMxDocument
  Dim pMap As IMap
  Dim pFeature As IFeature
  Dim pEnumFeature As IEnumFeature
  Dim pFeatureLayer As IFeatureLayer
  Dim pHyperlinkContainer As IHyperlinkContainer
  Dim pHyperlink As IHyperlink
  Dim LayerCount As Integer
  'Grab the first feature in the selection
  'and associate a hyperlink with it.  The
  'hyperlink is saved with the map document.
  Set pMxDoc = Application.Document
  Set pEnumFeature = pMxDoc.FocusMap.FeatureSelection
  Set pFeature = pEnumFeature.Next
  If pFeature Is Nothing Then Exit Sub
  Set pHyperlink = New Hyperlink
  pHyperlink.LinkType = esriHyperlinkTypeURL
  pHyperlink.link = "www.esri.com"
  pHyperlink.FeatureId = pFeature.OID
  'Find the feature layer the selected feature belongs to
  For LayerCount = 0 To pMxDoc.FocusMap.LayerCount - 1
    If TypeOf pMxDoc.FocusMap.Layer(LayerCount) Is IFeatureLayer Then
      Set pFeatureLayer = pMxDoc.FocusMap.Layer(LayerCount)
      If pFeatureLayer.FeatureClass Is pFeature.Class Then
        'Having found the feature layer, add the hyperlink
        Set pHyperlinkContainer = pFeatureLayer 'QI
        pHyperlinkContainer.AddHyperlink pHyperlink
        Exit Sub
      End If
    End If
  Next LayerCount
End Sub