Programmatically find the path of a layer in ArcMap


This VBA sample code shows the steps required to find the path of a layer in ArcMap.


  1. Get a reference to the first layer in your map.

    Option Explicit

    Sub GetPath()
    Dim pDoc As IMxDocument
    Dim pLayer As ILayer
    Set pDoc = ThisDocument
    Set pLayer = pDoc.FocusMap.Layer(0)

    pDoc.FocusMap.Layer(0) gets the first layer in the map. Layer(1) will return the second layer.

  2. Get access to the dataset. The method depends on what type of layer you have. This macro will work for feature, raster, and tin layers.

    Dim pDs As IDataset
    If (TypeOf pLayer Is IFeatureLayer) Or (TypeOf pLayer Is IRasterLayer) Then
    ' Query Interface to IGeoDataset, then to IDataset.
    Dim pGD As IGeoDataset
    Set pGD = pLayer
    'Qi from GeoDataset to Dataset
    Set pDs = pGD

    ElseIf (TypeOf pLayer Is ITinLayer) Then
    'Qi from Layer to TinLayer
    Dim pTinLayer As ITinLayer
    Set pTinLayer = pLayer
    Set pDs = pTinLayer.Dataset

    MsgBox "Layer is not a FeatureLayer, RasterLayer, or TinLayer. Exiting..."
    Exit Sub
    End If

    Query Interface (QI) retrieves other interfaces allowing access to the methods and properties on that interface.

  3. Use the PathName property on IWorkspace to return the Path Name for the layer.

    'Get the Workspace
    Dim pW As IWorkspace
    Set pW = pDs.Workspace
    MsgBox pW.PathName
    End Sub

Article ID:000002360

  • ArcMap 8 x

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Discover more on this topic