English

How To: Create a new graticule using VBA and ArcObjects

Summary

This article shows how to create a new graticule and set its properties in ArcMap.

Procedure

  1. Start ArcMap.
  2. Open the Visual Basic Editor.

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

  3. 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 module will only run in the current map document. To store the code in all your map documents, open the Normal.mxt ThisDocument code module.

  4. Copy the following into the code module.

    Code:
    Sub AddGraticule()

    Dim pMxDoc As IMxDocument
    Dim pGC As IGraphicsContainer
    Dim pElem As IElement
    Dim pMapFrame As IMapFrame
    Dim pMapGrids As IMapGrids
    Dim pMapGrid As IMapGrid
    Dim pActView As IActiveView
    ' Find the map frame
    Set pMxDoc = ThisDocument
    Set pGC = pMxDoc.PageLayout
    pGC.Reset
    Set pElem = pGC.Next

    Do While (Not pElem Is Nothing)
    If (TypeOf pElem Is IMapFrame) Then
    Set pMapFrame = pElem
    Exit Do
    End If
    Set pElem = pGC.Next
    Loop

    If (pMapFrame Is Nothing) Then
    MsgBox "Can't find a MapFrame"
    Exit Sub
    End If

    ' Create graticule object
    Set pMapGrid = New Graticule

    ' Set default parameters based on the map
    ' NOTE: If X and Y grid intervals are not set to non-zero by this
    ' or another means (e.g., via IMeasuredGrid), the graticule drawing
    ' procedure will hang indefinitely
    pMapGrid.SetDefaults pMapFrame

    ' The name will be shown in the Grids property page
    pMapGrid.Name = "My Graticule"

    ' Add graticule to the grid collection
    Set pMapGrids = pMapFrame
    pMapGrids.AddMapGrid pMapGrid

    ' Refresh the view
    Set pActView = pMxDoc.ActiveView
    pActView.Refresh

    End Sub

  5. Close the Visual Basic Editor.
  6. Run the code.

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