In this issue, we're going to create a very simple GIS tool to sum the area of only the selected polygons for the active layer in ArcMap. There is not a lot of error checking in this code - the point here is to learn how VBA interacts with the ArcObjects data model.
Before you begin
Start ArcMap (this script will work with ArcMap for ArcInfo 8.0.1, 8.0.2, beta 8.1 and ArcView beta 8.1). Add a single, polygon feature layer. This can be any polygon data source, from a Shapefile, Geodatabase, Coverage, or CAD file. Attributes do not matter.
Step 1 - Customize the Interface
In ArcMap, the first level of customization requires no code at all. All of ArcMap's tool bars can be docked or floating - standard stuff for the Windows NT interface. You can also add your own tool bars and tools.
- Right-click on any tool bar, and select Customize.
- From the Customize Dialogue window, select the Toolbars tab.
- Click on the New button, change the Toolbar Name to "My Tools" and select "Untitled" from the Save in dropdown.
- Click OK and you get a new, blank toolbar.
- Arrange your windows so you see your new toolbar and the Customize window.
- Switch to the Commands tab in the Customize window. Here you can select all the pre-defined tools for ArcMap.
- Scroll down the Categories list until you see the Pan/Zoom category.
- To add a tool to your new toolbar, simply drag and drop any tool. Drag the Full Extent, Pan, Zoom In and Zoom Out tools onto your My Tools toolbar.
- Change to the Categories to Selection, and drag the Select Features tool onto My Tools
- Close the Customize window, and experiment with your new toolbar.
Your "My Tools" toolbar should look something like this: 
Step 2 - Add a Custom Tool
In Step 1, all we did was rearrange the existing tools that come out-of-the-box with ArcMap. The next level of customization is to add custom tools using VBA code. To do that, we first create a new, empty tool, set some properties (like the icon), and then add the VBA source code.
Make the New Tool.
- Right-click on My Tools, and choose Customize. again
- From the Commands tab, scroll to the UIControls category.
- Click on the New UIControl button. Choose UIButtonControl as the UIControl Type, and click Create.
- You should have a new control listed called Project.UIButtonControl1. Drag this new tool onto the My Tools toolbar.
- Right click on the tool (on the new tool on the toolbar, not in the Customize window). Here you can set properties for the tool. From the context menu, choose and pick Change Button Image an icon for your tool (I like Rex the dog):
Add the VBA code. - Next, we need to add the VBA code that is to be executed when we click on our button tool. While you are still in customize mode, right-click on you new tool again, and choose View Source.
- This brings you into the Visual Basic Editor window. We don't have time or space to cover everything you have here, but suffice it to say this is where you build your VBA applications for ArcMap.
You should see a code window (or text box) that looks like this:
Two lines of code (the Private Sub and End Sub) are added for you. There are two dropdown lists - on the left for selecting the control (in this case, our new UIButtonControl1) and on the right, the event handler (in this case Click). Put these together, and it simply means, "When you Click on the UIButtonCOntrol1, execute this code. Let's enter the code. - Put your cursor between the Private Sub and End Sub lines, and start typing. The source code is below. Lines of code that begin with a single quote are comments. They are there to explain the code, but you don't have to type them in if you want to save keystrokes.
'ArcNorth News - Fall 2000 Edition
'Introducing VBA in ArcGIS - Volume 1
'c. ESRI Inc., ESRI Canada Limited. All rights reserved
'
'Adapted from:
"Exploring ArcObjects: The ESRI Guide to the Core ArcGIS Object Model"
'
'This unsupported code is for information purposes only, and
'is provided "as is"
Private Sub UIButtonControl1_Click()
'Initialize a reference to the map document
Dim pMxDoc As ImxDocument
Set pMxDoc = ThisDocument
' Initialize an Enumeration set (the selected features)
Dim pSelected As IenumFeature
Set pSelected = pMxDoc.FocusMap.FeatureSelection
' Move the pointer in the set to the top
pSelected.Reset
' Initialize a reference to a geographic entity
Dim pFeature As IFeature
Set pFeature = pSelected.Next
' Initialize an area object to store the polygon area
Dim pArea As Iarea
' Initialize a variable to hold the cumulative area
Dim totalArea As Double
' Loop through all of the selected polygons
Do While (Not pFeature Is Nothing)
' If it's a polygon, add it's area to the totalArea variable
If (pFeature.Shape.GeometryType = esriGeometryPolygon) Then
Set pArea = pFeature.Shape
totalArea = totalArea + pArea.Area
End If
' Otherwise, skip to the next feature
Set pFeature = pSelected.Next
Loop
' Report the final result to the user
MsgBox "Total Area for selected polygons = " & CStr(totalArea)
End Sub - Once you have typed in all the code (double check for typos), go to the File menu and select Close and Return to ArcMap.
- Back in ArcMap, use the Select Features tool from My Tools and drag a rectangle to select some polygons.
- Click once on you new UIButtonControl1 tool and your VBA code will report the total area for the selected polygons. Note, the reported area will be in whatever units your data set is in - square degrees if it's in Geographic, metres if it's in UTM and so on.
That's all there is to it. Your new tool (and the VBA code) will be saved with your ArcMap document (MXD).
from esri