HOW TO

Create a new graticule using VBA and ArcObjects

Last Published: October 2, 2020

Summary

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

Note:
These instructions pertain to ArcGIS 8.x. 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

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 only runs in the current map document. To store the code in all your map documents, open the Normal.mxt ThisDocument code module.
  1. Copy the following into the code module.
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 stop responding
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
  1. Close the Visual Basic Editor.
  2. Run the code.
    1. Click Tools > Macros > Macros to display the Macros dialog box.
    2. Select a macro and click Run.

Article ID:000004946

Software:
  • ArcMap 8 x

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Discover more on this topic