English

How To: Programmatically choose and add a North Arrow to the PageLayout

Summary

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

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'
  3. Right-click the UIButtonControl and select View Source.
  4. Copy the below code into the UIButtonControl's click event.
Code:
    
    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.