How to zoom to the extent of a selected layer


This sample zooms to the extent of the layer selected in the table of contents. In this case, use IContentsView::SelectedItem on the TOCDisplayView object.

How to use

  1. Paste the code into VBA.
  2. Modify the code to get the desired layer.
  3. Execute the routine.
[VBA]
Public Sub ZoomToLayer2()
    Dim pMxDoc As IMxDocument
    Dim pMap As IMap
    Dim pActiveView As IActiveView
    Dim pContentsView As IContentsView
    Dim pLayer As ILayer
    
    Set pMxDoc = Application.Document
    Set pMap = pMxDoc.FocusMap
    Set pActiveView = pMap
    Set pContentsView = pMxDoc.CurrentContentsView
    
    If Not TypeOf pContentsView.SelectedItem Is ILayer Then Exit Sub
    Set pLayer = pContentsView.SelectedItem
    pActiveView.Extent = pLayer.AreaOfInterest
    pActiveView.Refresh
End Sub