English

How To: Zoom to a bookmark using ArcObjects

Summary

This article contains code that shows how to zoom to a bookmark using VBA and ArcObjects.

Procedure

  1. Open ArcMap.
  2. Add data to ArcMap and create a bookmark.
  3. Open the Visual Basic Editor.

    In ArcMap, select Tools > Macros > Visual Basic Editor.

  4. In the Project Explorer window, expand Project.mxt and select ArcMap Objects > ThisDocument. Right-click and select View Code.
    [O-Image] Visual Basic Project  Explorer
    Note:
    Code in ThisDocument code module will only run in the current map document. To store the code in all map documents, open the code in the Normal.mxt ThisDocument code module.

  5. Paste the following code into the code module.

    Code:
    Sub ZoomToBookmark()
    Dim strBookmarkName As String
    strBookmarkName = "SouthwestUSA"

    Dim pDoc As IMxDocument
    Dim pMap As IMap
    Set pDoc = ThisDocument
    Set pMap = pDoc.FocusMap

    Dim pMapBookmarks As IMapBookmarks
    Dim pBookmarks As IEnumSpatialBookmark
    Dim pBookmark As ISpatialBookmark

    Set pMapBookmarks = pMap
    Set pBookmarks = pMapBookmarks.Bookmarks

    pBookmarks.Reset
    Set pBookmark = pBookmarks.Next
    Do Until pBookmark Is Nothing
    If pBookmark.Name = strBookmarkName Then
    pBookmark.ZoomTo pMap
    'Refresh the view
    pDoc.ActiveView.Refresh
    End If
    Set pBookmark = pBookmarks.Next
    Loop
    End Sub

  6. Set the value for strBookmarkName to the name of your bookmark.
  7. Close the Visual Basic Editor.
  8. Run the ZoomToBookmark subroutine.

    A. Click Tools > Macros > Macros to display the Macros dialog box.
    B. Select a macro and click Run.