English

How To: Activate a data frame programmatically in ArcMap

Summary

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

Procedure

 

  1. Start ArcMap
  2. Create a new UIButtonControl: How To: Create a new UIButtonControl
    Note:
    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.
     
    Code:
    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
      Else
        MsgBox "Please Select a Map"
        Exit Sub
     End If
    Else
      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
       Else
         Set pMxDoc.ActiveView = pMap
       End If
    End If