English

How To: Zoom to selected features at a specified scale

Summary

Information provided describes how to zoom in on a selected feature in ArcMap at a specified scale.

Note: 
The information in this article applies to ArcGIS versions 8.x and 9.x only. Visual Basic for Applications (VBA) for ArcMap and ArcCatalog has been deprecated at ArcGIS 10.

Procedure

  1. In ArcMap, navigate to Tools > Macros > Visual Basic Editor.
  2. On the left side of the Visual Basic Editor window, expand either the Normal (Normal.mxt) or the Project item.
  3. Expand the ArcMap Objects item.
  4. Select the ThisDocument module, right-click and select View Code.
  5. Copy and paste the following code:
    'zoom to selected feature at specified scale
    Sub ZoomSelectedToScale()
    
        Dim pDoc As IMxDocument
        Set pDoc = ThisDocument
        Dim pMap As IMap
        Set pMap = pDoc.FocusMap
        
        Dim pLayer As IFeatureLayer
        Dim pFSel As IFeatureSelection
        Set pLayer = pMap.Layer(0)
        Set pFSel = pLayer
        
        'Get the selected features
        Dim pSelSet As ISelectionSet
        Set pSelSet = pFSel.SelectionSet
        
        Dim pEnumGeom As IEnumGeometry
        Dim pEnumGeomBind As IEnumGeometryBind
        
        Set pEnumGeom = New EnumFeatureGeometry
        Set pEnumGeomBind = pEnumGeom
        pEnumGeomBind.BindGeometrySource Nothing, pSelSet
        
        Dim pGeomFactory As IGeometryFactory
        Set pGeomFactory = New GeometryEnvironment
        
        Dim pGeom As IGeometry
        Set pGeom = pGeomFactory.CreateGeometryFromEnumerator(pEnumGeom)
        
        'pDoc.ActiveView.Refresh
        pDoc.ActiveView.Extent = pGeom.Envelope
        pMap.MapScale = 2000   'change this value to the desired scale
        pDoc.ActiveView.Refresh
    
    End Sub
    Note:
    A shortcut button can be created to execute this code. 
    
    1. Select Tools > Customize to open the Customize dialog box.
    2. Select the Commands tab.
    3. Select [UIControls] from the Categories list box.
    4. Select Untitled from the Save In drop-down list to save the button to this map document. Select Normal to save the button to all ArcMap documents on the machine.
    5. Click New UIControl.
    6. Select UIButtonControl and click Create.
    7. Drag the new UIButtonControl to the toolbar of choice.
    8. Close the Customize dialog box 
  6. Change the value of 2000 in the line 'pMap.MapScale = 2000' to the desired scale.
  7. Go back to ArcMap. If necessary, add a layer first. Select a feature from the top layer using the Select Features tool.
  8. Execute the code by performing one of the following:
    • Go to Visual Basic Editor. Click anywhere in the body of the code. Press F5.
    • If a UIButtonControl was created to run the code, click the button.

Related Information