How To: Activate a data frame programmatically in ArcMap


This article contains a code sample that shows how to activate the selected data frame using VBA.



  1. Start ArcMap
  2. Create a new UIButtonControl: How To: Create a new UIButtonControl
    For more information on creating a UIControl, see the ArcGIS Desktop Help topic 'How to create custom commands with VBA'.
  3. Right-click the UIButtonControl and select View Source.
  4. Copy this code into the UIButtonControl's click event.
    Dim pMxDoc As IMxDocument
    Dim pMap As IMap
    Set pMxDoc = ThisDocument
    'Get the Currently selected map
    Dim pCV As IContentsView
    Set pCV = pMxDoc.CurrentContentsView
    Dim pVar As Variant
    If Not IsNull(pCV.SelectedItem) Then
      'Make sure the selected item is of type IMap
      If TypeOf pCV.SelectedItem Is IMap Then
         Set pMap = pCV.SelectedItem
        MsgBox "Please Select a Map"
        Exit Sub
     End If
      MsgBox "Please select a Map"
      Exit Sub
    End If
    'Check to see if the selected map is the focus map
    Dim pActiveView As IActiveView
    Set pActiveView = pMxDoc.ActiveView
    If Not pActiveView.FocusMap Is pMap Then
    'Check for Layout View or Map View
       If TypeOf pActiveView Is IPageLayout Then
         Set pMxDoc.ActiveView.FocusMap = pMap
         Set pMxDoc.ActiveView = pMap
       End If
    End If