HOW TO

Programmatically choose and add a North Arrow to the PageLayout

Last Published: April 25, 2020

Summary

Note:
This article pertains to ArcGIS versions 8.x. Later versions of ArcGIS may contain different functionality, as well as different names and locations for menus, commands and geoprocessing tools.

This VBA example illustrates how you can programmatically select a NorthArrow and place it on the PageLayout in a specified location.

Note: 
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. Create a new UIButtonControl: How To: Create a new UIButtonControl​
Note:
For more information on creating a UIControl, see the ArcGIS Desktop Help topic: 'Creating custom commands with VBA and UI Controls'
  1. Right-click the UIButtonControl and select View Source.
  2. Copy the below code into the UIButtonControl's click event.
    Dim pStyleGallery As IStyleGallery
    Dim pEnum As IEnumStyleGalleryItem
    Dim pStyleGalleryItem As IStyleGalleryItem
    
    Dim pMxDoc As IMxDocument
    Set pMxDoc = ThisDocument
    Set pStyleGallery = pMxDoc.StyleGallery
        
    Set pEnum = pStyleGallery.Items("North Arrows", "ESRI.Style", "Default")
        
    pEnum.Reset
        
    '***** Based on the index position of the North Arrow, the following lines of code select the arrow from the StyleGallery.
    '***** To see  the NorthArrow you want, click the Insert Menu and select NorthArrow
    '***** Change the number 5 (below) to the NorthArrow's position in the StyleSelector.
        
    Dim intX As Integer
    For intX = 1 To 5  ' Change '5' to the desired index number
        Set pStyleGalleryItem = pEnum.Next
    Next
    
    Dim pArrow As INorthArrow
    Set pArrow = pStyleGalleryItem.Item
        
    Call NorthArrowAdder(pArrow)
  1. Copy the below code into the same module as the UIButtonControl.
Code:
Public Sub NorthArrowAdder(pArrow As INorthArrow)

'***** This subroutine
'*****  -- creates the MapSurroundFrame
'*****  -- changes the NorthArrow color to red
'*****  -- displays the NorthArrow on the PageLayout

    Dim pMxDoc As IMxDocument
    Set pMxDoc = ThisDocument

   Dim pAv As IActiveView

    Dim pGC As IGraphicsContainer
    Set pGC = pMxDoc.ActiveView

    '***** A MapSurround refers to 'things' that enhance a map, for example,
    '***** ScaleBars, Legends, and NorthArrows.
    
    Dim pMapSurround As IMapSurround
    Set pMapSurround = pArrow  ' QI
        
    '*****  Color the NorthArrow red to illustrate that we have access to its properties.
    
    Dim pClr As IRgbColor
    Set pClr = New RgbColor
    pClr.Red = 255
    pArrow.Color = pClr
    
    '***** In order to be displayed  on a PageLayout, all MapSurrounds must reside in
    '***** a frame (MapsurroundFrame).
    
    Dim pMapSurroundFrame As IMapSurroundFrame
   
    '******************************************************************
    '***** CreateSurroundFrame ties the MapFrame to the MapsurroundFrame to
    '***** handle repositioning when moving/re-sizing.
    
    Dim pMapFrame As IMapFrame
    Set pMapFrame = pGC.FindFrame(pMxDoc.Maps.Item(0))
    
    Dim pUid As UID
    Set pUid = New UID
    pUid.Value = "esricore.MarkerNorthArrow"
    
    Set pMapSurroundFrame = pMapFrame.CreateSurroundFrame(pUid, pArrow)
    '********************************************************************
    '********************************************************************
    
    Set pAv = pMxDoc.ActiveView
    
    '***** QI and set an IElement interface. This interface give you acces to a
    '***** Geometry property. This geometry property (in this case) takes a rectangle/envelope
    '***** that represents a location on the PageLayout where the NorthArrow will be stored...
    Dim pElem As IElement
    Set pElem = pMapSurroundFrame
  
    '***** The 'TheGeometry' function returns an envelope.
    pElem.Geometry = TheGeometry

    '***** Finally add the element to the graphicsContainer
    pGC.AddElement pElem, 0
    pMxDoc.ActiveView.Refresh
End Sub

Public Function TheGeometry() As IGeometry
'***** This function creates a geometry that is used by the PageLayout as the
'***** position to display the North Arrow.
    
    Dim pEnv As IEnvelope
    Set pEnv = New Envelope
    
    Dim pt1 As IPoint
    Set pt1 = New Point
    pt1.X = 1.5
    pt1.Y = 1.5
    
    Dim pt2 As IPoint
    Set pt2 = New Point
    pt2.X = 1.5
    pt2.Y = 1.5
    
    pEnv.LowerLeft = pt1
    pEnv.UpperRight = pt2
    
    Set TheGeometry = pEnv
 
End Function
  1. Click View and select Layout View to set the PageLayout as the Active View.
  2. Click the button to run the code.

Article ID:000005677

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