Keep a layer visible with ArcObjects VBA

Last Published: April 25, 2020


The code sample provided can be used to keep the top layer turned on. It is possible to customize it to work with another layer. This essentially disables turning off the layer.


  1. Start ArcMap.
  2. Open the Visual Basic Editor.

    In ArcMap, select Tools > Macros > Visual Basic Editor.

  3. In the Project Explorer window, expand Project and select ArcMap Objects > ThisDocument then right-click and select View Code.
    [O-Image] Visual Basic Project  Explorer

    Code in the Project's ThisDocument code module will only run in the current map document. If you want to store the code in all your map documents open the Normal.mxt ThisDocument code module instead.

  4. Copy the following code into the code module.
    Dim m_pMxDoc As IMxDocument
    Dim m_pMap As IMap
    Dim m_pLayer As ILayer
    Dim WithEvents m_pLyrEvents As FeatureLayer
    Private WithEvents m_pActiveViewEvents As Map

    Private Function MxDocument_OpenDocument() As Boolean
    Set m_pMxDoc = Application.Document
    Set m_pMap = m_pMxDoc.FocusMap
    Set m_pLyrEvents = m_pMap.Layer(0) 'change this number if you wish to work with a different layer
    Set m_pActiveViewEvents = m_pMxDoc.FocusMap
    End Function

    Private Sub m_pActiveViewEvents_ViewRefreshed(ByVal view As esriCarto.IActiveView, _
    ByVal phase As esriCarto.esriViewDrawPhase, ByVal data As Variant, ByVal envelope As esriGeometry.IEnvelope)
    Set m_pLayer = m_pLyrEvents
    m_pLayer.Visible = True
    End Sub

    Private Sub m_pLyrEvents_VisibilityChanged(ByVal currentState As Boolean)
    Set m_pLayer = m_pLyrEvents
    If currentState = False Then
    m_pLayer.Visible = True
    Exit Sub
    Exit Sub
    End If
    End Sub

    Sub StopListening()
    Set m_pLyrEvents = Nothing
    Set m_pActiveViewEvents = Nothing
    End Sub

  5. Close the Visual Basic Editor.
  6. Save and close the ArcMap document.
  7. Open the same ArcMap document.
  8. Click the layer visibility checkbox for the top layer. You should see that it is rendered useless.

Article ID:000008786

  • ArcMap 9 x

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options