How to add a layer from a file


This tip shows you how to add a .lyr file as a layer to your dataframe.

How to use

  1. Copy-paste this procedure into the VB Editor in ArcMap.
  2. Change the location of the .lyr file to correspond to your ArcGIS installation.
  3. Run the procedure.
[VBA]
Private Sub AddLayerFromFile()
    Dim pApp As IApplication
    Dim pMxDoc As IMxDocument
    Dim pMap As IMap
    Dim pGxLayer As IGxLayer
    Dim pGxFile As IGxFile
    Dim strLayerPath As String
    
    Set pGxLayer = New GxLayer
    Set pGxFile = pGxLayer
    
    ' Set this variable to the location of you layer file
    strLayerPath = "D:\ArcGIS\SampleMaps\Data\JoshuaTreeNP\Dem.lyr"
    
    pGxFile.Path = strLayerPath
    
    If Not pGxLayer.Layer Is Nothing Then
        Set pApp = Application
        Set pMxDoc = pApp.Document
        Set pMap = pMxDoc.FocusMap
        pMap.AddLayer pGxLayer.Layer
    End If
End Sub