How to create a map grid with default properties


You can use this sample's code to quickly create a map grid of your choice - index grid, measured grid or graticule - using a map grid factory.

How to use

  1. Copy-paste this sample's code into a module in your Visual Basic Editor.
  2. Run the procedure. This will create an index grid for your currently selected data frame.
  3. Alternatively, you can create a graticule or a measured grid. You can choose between these options in the CreateMapGrid procedure. Comments in the procedure provide more info on how to do this.
[VBA]
Private Sub CreateMapGrid()
    Dim pMxDoc As IMxDocument
    Dim pMap As IMap
    Dim pGraphicsContainer As IGraphicsContainer
    Dim pMapFrame As IMapFrame
    Dim pActiveView As IActiveView
    Dim pMapGrids As IMapGrids
    Dim pMapGrid As IMapGrid
    
    Set pMxDoc = ThisDocument
    Set pMap = pMxDoc.FocusMap
    Set pGraphicsContainer = pMxDoc.PageLayout
    Set pMapFrame = pGraphicsContainer.FindFrame(pMap)
    
    'Create map grid
    Dim pMapGridFactory As IMapGridFactory
    'Choose between one of the following grid types
    'by uncommenting your choice and commenting out the others
    Set pMapGridFactory = New IndexGridFactory
    ''Set pMapGridFactory = New GraticuleFactory
    ''Set pMapGridFactory = New MeasuredGridFactory
    
    Set pMapGrid = pMapGridFactory.Create(pMapFrame)
    
    'Add map grid to Layout and refresh
    Set pMapGrids = pMapFrame
    pMapGrids.AddMapGrid pMapGrid
    Set pActiveView = pMxDoc.PageLayout
    pActiveView.PartialRefresh esriViewBackground, Nothing, Nothing
    
End Sub