Activate a data frame programmatically in ArcMap

Last Published: April 25, 2020


This article pertains to ArcGIS versions 8.x. Later versions of ArcGIS may contain different functionality, as well as different names and locations for menus, commands and geoprocessing tools.

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

Support for Visual Basic for Applications (VBA) for ArcMap and ArcCatalog ended with the ArcGIS 10.2.2 release, and Esri has not included VBA compatibility setups since version 10.5. See: ArcGIS Desktop and VBA Moving Forward


  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'.
  1. Right-click the UIButtonControl and select View Source.
  2. 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

Article ID:000003545

  • ArcMap 8 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