Create a graphics layer programmatically in ArcMap using VBA ArcObjects


This article contains sample code that creates and populates a graphics layer in ArcMap using VBA and ArcObjects.


  1. Open 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

    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.

  4. Paste the following code into the code module.

    Public Sub AddPointToGraphicsLayer()

    Dim pMxDoc As IMxDocument
    Set pMxDoc = ThisDocument
    Dim pMxApp As IMxApplication
    Set pMxApp = Application
    Dim pMap As IMap
    Set pMap = pMxDoc.FocusMap

    Dim pCGLayer As ICompositeGraphicsLayer
    Set pCGLayer = New CompositeGraphicsLayer

    'Name the new graphics layer
    Dim pLayer As ILayer
    Set pLayer = pCGLayer
    pLayer.Name = "TestPoint"

    'Add the layer to the map
    pMap.AddLayer pCGLayer

    'set xy location for point
    Dim x As Double
    Dim y As Double
    x = 200
    y = 200

    'Make a point
    Dim pPnt As IPoint
    Set pPnt = New Point
    pPnt.x = x
    pPnt.y = y

    'Set color and symbol for the point
    Dim pColor As IRgbColor
    Set pColor = New RgbColor
    pColor.Blue = 255
    pColor.Green = 0
    pColor.Red = 0
    Dim pSimpleMarkerSymbol As ISimpleMarkerSymbol
    Set pSimpleMarkerSymbol = New SimpleMarkerSymbol
    With pSimpleMarkerSymbol
    .Style = esriSMSCircle
    .Size = 4
    .Color = pColor
    End With

    'Create a marker element
    Dim pMarkerElement As IMarkerElement
    Set pMarkerElement = New MarkerElement
    pMarkerElement.Symbol = pSimpleMarkerSymbol
    Dim pElement As IElement
    Set pElement = pMarkerElement
    pElement.Geometry = pPnt

    'Get the graphics layer and screen display
    Dim pGrLayer As IGraphicsLayer
    Set pGrLayer = pCGLayer

    Dim pScreenDisplay As IScreenDisplay
    Set pScreenDisplay = pMxApp.Display

    'Add the marker element ot the layer graphics container
    Dim pGraphicCont As IGraphicsContainer
    Set pGraphicCont = pGrLayer
    pGraphicCont.AddElement pMarkerElement, 0
    With pScreenDisplay
    .ActiveCache = 0
    .StartDrawing pScreenDisplay.hDC, 0
    .SetSymbol pSimpleMarkerSymbol
    pElement.Draw pScreenDisplay, Nothing
    End With

    'Refresh/redraw the display with the new point

    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.

Article ID:000004962

  • ArcMap 8 x

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Related Information

Discover more on this topic