How To: Programmatically add a neatline to the active data frame in ArcMap


This article contains sample code that adds a neatline to the active data frame.



  1. Open ArcMap.
  2. Create a new UIButtonControl: How To: Create a new UIButtonControl
    For more information on creating a UIControl, see the ArcGIS Desktop Help topic: 'Creating custom commands with VBA and UI Controls'
  3. Right-click the UIButtonControl and select View Source.
  4. Paste this code into the UIButtonControl's click event.
    Sub AddNeatline()
    Dim x As Double
    'Change x to equal the distance of
    'the neatline from extent envelope
    x = 0.5
    Dim pMxDoc As IMxDocument
    Dim pMxApp As IMxApplication
    Dim pActiveView As IActiveView
    Dim pMap As IMap
    Dim pPageLayout As IPageLayout
    Dim pDisplay As IScreenDisplay
    Dim pGC As IGraphicsContainer
    Dim pElem As IElement
    Dim pMapFrame As IMapFrame
    Dim pEnv As IEnvelope
    Dim pElemEnvelope As IEnvelope
    Set pMxApp = Application
    Set pMxDoc = ThisDocument
    Set pPageLayout = pMxDoc.PageLayout
    Set pGC = pPageLayout
    Set pDisplay = pMxApp.Display
    'Get Active Map name
    Dim strMapName As String
    strMapName = ""
    Set pMap = pMxDoc.FocusMap
    strMapName = pMap.Name
    'Get the active map's Map Frame out of the graphics container
    Set pElem = pGC.Next
    Do While (Not pElem Is Nothing)
        If (TypeOf pElem Is IFrameElement) Then
            If (TypeOf pElem Is IMapFrame) Then
                Set pMapFrame = pElem
                If strMapName = pMapFrame.Map.Name Then
                    Set pElemEnvelope = pElem.Geometry.Envelope
                    Set pEnv = pMapFrame.MapBounds
                End If
            End If
        End If
        Set pElem = pGC.Next
    Dim pRect As IRectangleElement
    Set pRect = New RectangleElement
    Dim pElement As IElement
    Set pElement = pRect
    'Set the Neatline distance from the frame border
    pElemEnvelope.XMin = pElemEnvelope.XMin - x
    pElemEnvelope.YMin = pElemEnvelope.YMin - x
    pElemEnvelope.XMax = pElemEnvelope.XMax + x
    pElemEnvelope.YMax = pElemEnvelope.YMax + x
    pElement.Geometry = pElemEnvelope
    'Set the Neatline symbology
    Dim pRectSym As IFillShapeElement
    Set pRectSym = pRect
    'Set the style of the fill symbol
    Dim pFillSymbol As ISimpleFillSymbol
    Dim plinesymbol As ISimpleLineSymbol
    Set pFillSymbol = New SimpleFillSymbol
    Set plinesymbol = pFillSymbol.Outline
    pFillSymbol.Style = esriSFSHollow
    'Set the outline symbol
    plinesymbol.Style = esriSLSSolid
    plinesymbol.Width = 6
    pFillSymbol.Outline = plinesymbol
    'Set it all into the element
    pRectSym.Symbol = pFillSymbol
    'Draw the neat line
    pElement.Activate pDisplay
    pGC.AddElement pRect, 0
    End Sub
  5. Close the Visual Basic Editor.
  6. Switch to layout view and press the UIButtonControl to insert a neatline into the active data frame.