HOW TO
This article contains sample code that adds a title to your page layout. The code uses the name of the first layer in the map document for the title.
Note:
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.
Code:
Sub AddTitleToLayout()
Dim pApp As IMxApplication
Dim pDoc As IMxDocument
Dim pMap As IMap
Dim pLayer As ILayer
Dim pPageLayout As IPageLayout
Dim pGc As IGraphicsContainer
Dim pDisplay As IScreenDisplay
Dim pTxtElem As ITextElement
Dim pTxtSym As ITextSymbol
'Get the first layer in the map
Set pApp = Application
Set pDoc = ThisDocument
Set pMap = pDoc.FocusMap
Set pLayer = pMap.Layer(0)
'Get the layer name to use for layout title
Dim sStrName As String
sStrName = pLayer.Name
Set pPageLayout = pDoc.PageLayout
Set pDisplay = pApp.Display
Set pGc = pPageLayout
pGc.Reset
'Set the font and color properties
'for the title
Dim myFont As IFontDisp
Set myFont = New StdFont
myFont.Name = "Courier New"
myFont.Size = 24
Dim myColor As IRgbColor
Set myColor = New RgbColor
myColor.Red = 150
myColor.Green = 0
myColor.Blue = 0
'Create a text element
Set pTxtElem = New TextElement
'Create a text symbol
Set pTxtSym = New TextSymbol
With pTxtSym
.Color = myColor
.Font = myFont
End With
'Set symbol property
pTxtElem.Symbol = pTxtSym
'set the text property to be the layer's name (Uppercase)
pTxtElem.Text = UCase(sStrName)
'Create an envelope for the TextElements Geometry
Dim pEnv As IEnvelope
Set pEnv = New Envelope
Dim pPoint As IPoint
Set pPoint = New Point
pPoint.X = 2
pPoint.Y = 8
pEnv.LowerLeft = pPoint
pPoint.X = 7
pPoint.Y = 10
pEnv.UpperRight = pPoint
'set the text elements geomtery
Dim pElem As IElement
Set pElem = pTxtElem
pElem.Geometry = pEnv
'Add the element to the graphics container
pGc.AddElement pElem, 0
pDoc.ActiveView.Refresh
End Sub
Article ID:000004979
Get help from ArcGIS experts
Download the Esri Support App