English

How To: Set visibility for all the layers in ArcMap to false

Summary

Instructions provided describe how to create a button that sets the visibility for all layers in the ArcMap Table of Contents to false, using VBA and ArcObjects.

Procedure

 

  1. Open ArcMap.
  2. Open the Visual Basic Editor. In ArcMap, select Tools > Macros > Visual Basic Editor.
  3. Create a new UIButtonControl: How To: Create a new UIButtonControl​
    Note:
    For more information on creating a UIControl, see the ArcGIS Desktop Help topic: 'Creating custom commands with VBA and UI Controls'
  4. Right-click the UIButtonControl and select View Source.
  5. Paste this code into the UIButtonControl's click event:
     
    Code:
    Public Sub LayerVis()
    
    Dim pMxDoc As IMxDocument
    Dim pMap As IMap
    Dim pFeatureLayer As IFeatureLayer
    Dim pGroupLayer As IGroupLayer
    Dim i As Integer
    
    'get the document and map
    Set pMxDoc = ThisDocument
    Set pMap = pMxDoc.FocusMap
    
    'loop through the layers and make them visible or not
    
    For i = 0 To pMap.LayerCount - 1
    If TypeOf pMap.Layer(i) Is IGroupLayer Then
        Set pGroupLayer = pMap.Layer(i)
        pGroupLayer.Visible = False
        ElseIf TypeOf pMap.Layer(i) Is IFeatureLayer Then
        Set pFeatureLayer = pMap.Layer(i)
        pFeatureLayer.Visible = False
        End If
      Next i
    
      'refresh the map
      pMxDoc.ActiveView.Refresh
    
      'refresh the table of contents
      pMxDoc.UpdateContents
    
    End Sub