How to add a layer file to the map


This tip shows how to add a layer file to an open map document programmatically.

How to use

  1. Copy the subroutine to VBA Editor of an opened map document and change the filePath accordingly.
  2. Execute the subroutine.
[VBA]
Public Sub AddLayerFileToMap()
    Dim filePath As String
    filePath = "C:\Program Files\ArcGIS\DeveloperKit10.0\Samples\data\USA\states.lyr"
    Dim pGxLayer As IGxLayer
    Dim pGxFile As IGxFile
    
    Set pGxLayer = New GxLayer
    Set pGxFile = pGxLayer
    pGxFile.Path = filePath
    Dim pMxDoc As IMxDocument
    Set pMxDoc = ThisDocument
    pMxDoc.FocusMap.AddLayer pGxLayer.Layer
End Sub