How to report the number of selected elements


Use the code below to report, in the ArcMap StatusBar, the number of currently selected elements in a PageLayout.

How to use

  1. Open ArcMap and paste the code below into the Project code window in VBA.
  2. In ArcMap, run the SetEvents macro.
  3. Now select some graphic elements in PageLayout view. The count of selected elements will be reported in the status bar.
[VBA]
Private WithEvents m_pSelEvents As ElementSelection
Private WithEvents m_pDoc As MxDocument
Private m_pMxDoc As IMxDocument

Public Sub SetEvents()
    '
    ' This function sets up the event handlers for the Document and ElementSelection.
    '
    Set m_pDoc = ThisDocument
    Set m_pMxDoc = ThisDocument
    If TypeOf m_pMxDoc.ActiveView Is IPageLayout Then
        Set m_pSelEvents = m_pMxDoc.ActiveView
    Else
        Set m_pSelEvents = Nothing
    End If
End Sub

Private Sub m_pSelEvents_SelectionChanged()
    '
    ' If the current tool is the SelectGraphics tool, we report to the user the
    ' count of currently selected graphics in the layout.
    '
    If Application.CurrentTool.Name = "PageLayout_SelectTool" Then
        Dim pGraphSel As IGraphicsContainerSelect
        Set pGraphSel = m_pMxDoc.ActiveView
        Application.StatusBar.Message(0) = "Selected Graphics: " & pGraphSel.ElementSelectionCount
    End If
End Sub

Private Function m_pDoc_ActiveViewChanged() As Boolean
    '
    ' When the user changes ActiveView, we refresh the m_pSelEvents variable.
    ' We also call the SelectionChanged event to begin with to report the
    ' count of graphics selected initially.
    '
    If TypeOf m_pMxDoc.ActiveView Is IPageLayout Then
        Set m_pSelEvents = m_pMxDoc.ActiveView
        m_pSelEvents_SelectionChanged
    Else
        Set m_pSelEvents = Nothing
    End If
End Function