You are on page 1of 9

Geodetic Science 634 - Digital Mapping Systems Winter 2007 Laboratory Exercise No. 4.

ArcObjects and VBA Programming
Date assigned: Jan 30, 2007 Date due: February 13, 2007 Objectives: 1. 2. To customize the graphical user interface (GUI) of ArcGIS, To learn program VBA with ArcObjects.

Introduction: This lab will help you become familiar with ESRI application developer experience. First, you will see how to control and modify the graphical user interface (GUI) of ArcMap and ArcCatalog. Next, you will use the development environment that’s embedded in ESRI applications, Visual Basic for Applications (VBA), to create your own commands. At the end of the lab, you will go through a tutorial with MapObjects using Visual Basic. This lab assumes some familiarity with programming with Visual Basic and terms used to describe it. Part 1. Customizing the user interface Although ArcGIS are designed to be flexible and easy to use, you may want the ArcMap and ArcCatalog interfaces to reflect your own preferences and the way you work. You can customize ArcMap and ArcCatalog in many ways. Here are some examples: • • • • Position toolbars in a specific area of the application. Group commands in a way that works best for you. Add new macros or load custom commands from another source. Always work with the same geographic data.

In this part, you will go through a tutorial and learn how to customize the user interface. a. Tutorial Start ArcMap and add street feature class to the map. • 1. Manipulating toolbars using the Customize dialog In the Tools menu click Customize. The Customize dialog appears. You can also double-click any unoccupied area of any toolbar to display the Customize dialog. Click the Toolbars tab. The presence or absence of a check mark next to the toolbar name indicates its visible state, just as the in the Toolbars submenu of the View menu. You can click the check mark next to any toolbar to turn it off and on. Creating a new toolbar In the Toolbars tab of the Customize dialog, click the New button. In the dialog that appears, specify My Tools as the name of the new toolbar or use the default setting.


• 3. 4.

is visible. 22. In the Customize dialog. 10. 21. 11. choose Selection. Drag the Fixed Zoom In. • Removing buttons from a toolbar.5. In the dialog that appears. Display the Customize dialog. Selection. and Page Layout categories. • Renaming a toolbar 13. Select the Pan/Zoom category from the Categories list at the left of the dialog. Drag and drop it to the right of the Zoom Out button on the My Own Tools toolbar. Click the Commands tab and choose the Menus category from the Categories list at the left-hand side of the dialog. 19. Display the Customize dialog. 9. Display the Customize dialog. Store the toolbar in the document by changing the name of the Save in the dropdown list from Normal. 17. Click the Rename button. Continue adding some commands from the Pan/Zoom. Note that you can only rename toolbars you’ve created. 15. Make sure the toolbar you just renamed. until you have your own version of the built-in Tools toolbar. click Cancel. 16. • 7. Fixed Zoom Out. 12. Click OK. 6. In this case. 20. Click OK. 2 . In the Commands list at the right-hand side of the dialog. select My Tools. they are still available in the Customize dialog. Select the Zoom in command and drag it to the My Toolbar. 14. • Even though you’ve removed the buttons from the toolbar. specify My Own Tools as the new name. You can also dock the toolbar or drag it to wherever you want. Make sure the toolbar named My Own Tools is visible. Click the Commands tab of the Customize dialog. My Tools. In the Toolbars tab. Adding buttons to a toolbar Make sure the toolbar you just created. 8. Adding a menu to a toolbar 18.mxt to Untitled or the name of the current project. is visible. Scroll to the bottom of the Commands list at the right of the dialog. You can resize the toolbar so that it width allows the display of two commands per column. My Own Tools. The newly created toolbar appears near the top of the application window. If you choose not to rename the toolbar. click Close. select the name of the toolbar whose name you want to change. Full Extent buttons off the dialog.

In the Macros dialog. Learning VBA fundamentals and programming with ArcObjects. VBA is not a standalone program. Task Create another toolbar containing menu and command buttons. that lets you write a Visual Basic (VB) macro and then debug and test it right away in ArcMap or ArcCatalog. Both ArcMap™ and ArcCatalog™ come with Visual Basic for Applications (VBA). Part 2. Enter the following code to MyZoomIn: Sub MyZoonIn( ) ‘ ‘ macro: MyZoomIn ‘ Dim pDoc as IMxDocument Dim pEnv as IEnvelope Set pDoc = ThisDocument 3 . you can export a module to a . Tutorial • 1. navigate to N:\gs634 or any other folder you like. a new module with that name is created for you and added to the VBA project. 3. In ArcCatalog.bas file from. You can organize your macros in different modules. a new module called "NewMacros" is created. Next time when you create a new map. click Save As. type the module name before the macro's name. Macros can be stored in any of the VBA projects depending on where you want the new functionality to be available.mxt). with the extensive object library that ArcMap and ArcCatalog expose. Type the template name. You add code to the procedure in a Code window just as you would in VB.mxt). its VBA project is called TemplateProject (NameOfTemplate. It provides an integrated programming environment. each module has its own Code window. and if another template is loaded.gxt). type MyZoomIn in the Macro name text box and then click Create.mxt). The application creates a new module named Module1 and stubs in the Sub procedure. if you provide a name for a new macro but don't specify which module to store it in.• Saving changes to a template You can save your work to a document or template. the VBA project for the Normal template is called Normal (Normal. 25.mxd). such as using message boxes for input. it's embedded in the applications. Name it as GS634 Tools. b. Using modules makes it easier to share your VB code with others. for example. the Visual Basic Editor (VBE). To add your macro to a specific module. When you create a macro. Changes save to a document are specific to the document. there is only a VBA project for the Normal template. each document and template that is currently loaded has an associated VBA project in the VBE. you may open this template and use the toolbar you created here. If the module doesn't exist. whereas changes saved to a template will be reflected in all document based on the template. In the Save As dialog. your VBA project. and import a .bas file to. choose ArcMap Templates (*. "Department.WorkMacro". 23. 24. When you create a new macro in the Macros dialog box. 2. The VBA project for the document is called Project (NameOfDoc. In the File menu. select Macros and then Macros. In ArcMap. Similarly. The procedure's name is the name you assign to the macro. you're creating a VB Sub Procedure. precede the macro's name with the name of the module to store it in. A macro can integrate some or all of VB's functionality. called Normal (Normal. a. Creating a macro In the Tools menu. and then click Save. The ESRI Object Library is always available to you in the VBA environment.

ActiveView. you can navigate directly to the Visual Basic Editor and create procedures on your own. the Visual Basic Editor’s keyboard accelerator.Set pEnv = pDoc. • Adding a macro to a tool bar In the Tools menu.5. • 7. 10. Press Alt+F11.Extent pEnv. select Customize. The second line declares a variable that represents a rectangle with sides parallel to a coordinate system defining the extent of the data. 19. True pDoc.5. 9. Paste the MyZoomIn Sub code below the existing code. click the Project entry. 0.5. 11. 20. the macro zooms in on the map’s features once the screen display is refreshed. 13. Follow steps 7-15 to add and run your second macro. 15. create a new toolbar. 22. 4. Invoking the Visual Basic Editor directly and getting help in the Code Window As an alternative to the Create button in the Macro dialog. 5. Select the NewMacros. ThisDocument. Then display zooms in. In the context menu that appears. 4 . In the Tools menu.MyZoomIn macro and click Run.5.Expand 0. 16. right-click the icon. and then Module1. In the Toolbars tab. 8.5.Expand 0. The predefined variable. True to pEnv. In the Code Window. By reducing the size of the envelope that represents the extent of the map. 14. Change the line pEnv. select Project Explorer. select Macros and then Macros. To change its properties. In the Project Explorer. 12. 18. 21. 0.ActiveView. In the Save in combo box. 1.Extent = pEnv pDoc. The ActiveView property provides an IActiveView interface that links the document data to the current screen display of that data. 17. then Modules.ActiveView. Click the button to run the macro. Switch back to ArcMap. select the name of your project.Expand 1. Close the Customize dialog.Refresh End Sub The first line of the macro declares a variable that represents the ArcMap document. Switch to the Commands tab and select the Macros category. The commands list at the right of the dialog lists NewMacros. 6. Drag the macro name to the toolbar you created. Change the name of the copied Sub to MyZoomOut. is the IDocument interfaces to the MxDocument object that represent the ArcMap document.MyZoomIn. In the VBE’s View menu. The macro appears with a default icon.5. True 23. copy the MyZoomIn code from the beginning of the Sub to the End Sub. select Change Button Image and choose a button from the palette of icons.

In the Customize dialog. you can consult ArcObjects Developer Help in the ArcInfo program group for object model diagrams. The code use Envelope object that the method returns to set the new extent for the map. The ArcObjects Developer Help window displays the help topic for Expand. samples. Click New UIControl. select commands tab and then change the Save in the combo box to the name of your project or Untitled. choose UIToolControl as the UIControl Type and then click Create and Edit.TrackNew(pScreenDisp. 4.Extent = pEnv pDoc. once invoked. Nothing) pDoc.5. Add the following code to the MouseDown event procedure. and tricks. which rubber-bands a new shape on the specified screen. True” in the Code Window of VBE. 24. you’ve only created commands.You can use ArcObjects Developer Help in the ArcInfo program group to learn more about the methods and functions you used above. 0. You’ll add code to this event to enable you to drag a rectangle on the screen display. Commands. 1. the application will zoom to the rectangle’s extent. Dim pDoc as IMxDocument Set pDoc = ThisDocument 5 . You may get information about object model diagrams. Go to Start->All programs->original programs->ArcGIS.Expand 0. As you’ve seen in the build-in toolbars and menus. you can add sophisticated controls to toolbars and menus. In the Categories list. 3. As part of the customization environment. instead. In the dialog that appears. Add the following code to the UIToolControl’s Enabled event procedure. tips. • In the Tools menu. 5.ActiveView. In addition to ArcObjects Help. select UIControls.ActiveView.5.ScreenDisplay Set pRubberEnv = New RubberEnvelope Set pEnv = pRubberEnv. and tricks. 2. 2. Adding code for the UIToolControl The application adds an entry in the Object Box for a UIToolControl and stubs in an event procedure for the UIToolControl’s Select event. You won’t add any code to the Select event procedure at this time. • Creating a tool in VBA To this point in the tutorial. tips. select the MouseDown event in the Events/Procedures box at the right-hand side of the Code Window. select Customize. Such as the command “pEnv. samples. click ArcObjects Developer Help. These controls are called UIControls.Refresh End If The key line of this procedure is the one containing the TrackNew method. If button = 1 Then Dim pDoc As IMxDocument Dim pScreenDisp As IScreenDisplay Dim pRubberEnv As IRubberBand Dim pEnv As IEnvelope Set pDoc = ThisDocument Set pScreenDisp = pDoc. usually perform some direct action without user intervention. 1. users interact with other controls in addition to commands.ActiveView.

you will be given some VB codes about programmatically adding a line event layer to ArcMap. Having multiple data frames in ArcMap is the equivalent of having multiple views in ArcView GIS 3. If you are importing a layout. • Close VBE and return to ArcMap. Start ArcMap and open a blank map document. Add this UIControl to the toolbar you created above.mxd). The selected import layout is now the layout view in ArcMap. add the following code to the ToolTip event procedure. Import ArcView Project into ArcMap Many items from an ArcView GIS 3 project (. The imported views are now separate data frames located in the table of contents.apr) can be imported to an ArcMap 8. Click the File menu and click Import from ArcView project. Some graphic and text adjusting may be required to get the desired look for the map layout. To import multiple layouts from an ArcView GIS 3. Add the following code to the CursorID event procedure to control the cursor that appears when you use the tool. The views that are in the layout will be automatically selected. Add comments for each line in the codes. 3. To activate a different data frame. 7.mxd). Navigate to and select the ArcView project you would like to import to ArcMap. Views that are not in the selected layout may be selected interactively. 6 . Importing ArcView projects with multiple layouts requires that you choose only one layout to import.LayerCount > 0 Then UIToolControl1_Enabled = True ElseIf pDoc. Test the tool. The active data frame will be visible in the view and will be the only data frame with the title in bold letters. Note: • • ArcMap does not support multiple layouts in a document.FocusMap. 2. Tasks In this part. Your task is to turn these codes into one command button of your own and add it into the toolbar you created. 4. Move from layout view to data view to see a single data frame in the window. ‘Crosshair To provide a ToolTip when the mouse hovers over the tool’s button.If pDoc. import each of the layouts in your ArcView 3. Click OK to import the project. Add the following code to the Message event procedure to supply additional text describing the tool’s functionality in the Status bar area. UIToolControl1_CursorID = 3 4.x project. choose the layout you would like to import from the Layouts dropdown list. right-click the data frame's title in the table of contents and select Activate from the list of options.LayerCount = 0 Then UIToolControl1_Enabled = False End If 3. 1. • b. 8. UIToolControl1_Message = “Zoom to a designated rectangle” 6.3 project (. UIToolControl1_ToolTip = “Zoom to rectangle” 5.x project into separate ArcMap map documents (.Focusmap. You can change the icon of the button.

It is called fare.FeatureClass Exit For End If End If Next i If pRouteFc Is Nothing Then MsgBox "Could not find the route feature class". vbExclamation. Add a route layer to ArcMap called 'Bus' (or change the code accordingly).LayerCount . It is called 'Bus'.How to use the code: 1.BrowseName) = "fare" Then Set pEventTable = pDS Exit For End If Next i If pEventTable Is Nothing Then MsgBox "Could not find event table". Dim pLayer As ILayer Dim pFLayer As IFeatureLayer Dim pRouteFc As IFeatureClass For i = 0 To pMap.TableCount . 4. If necessary. vbExclamation.Table(i) If LCase(pDS. Add a table called 'fare' to ArcMap (or change the code accordingly).Layer(i) If LCase(pLayer. "AddLineEventLayer" Exit Sub End If 7 .1 Set pDS = pTblColl. 2. "AddLineEventLayer" Exit Sub End If '+++ Get the route feature class.Name) = "Bus" Then If TypeOf pLayer Is IFeatureLayer Then Set pFLayer = pLayer Set pRouteFc = pFLayer.1 Set pLayer = pMap. Run the code. 3. Dim pMxDoc As IMxDocument Dim pTblColl As ITableCollection Dim pEventTable As ITable Dim i As Long Dim pMap As IMap Dim pDS As IDataset Set pMxDoc = ThisDocument Set pMap = pMxDoc. change the field names in the code to fit your data.FocusMap Set pTblColl = pMap For i = 0 To pTblColl. Public Sub AddLineEventLayer() '+++ VBA code that shows how to add a line RouteEventSource as a layer in the Map On Error GoTo eh '+++ Get the event table. Paste the code into VBA.

Dim pEventFC As IFeatureClass Set pName = pRESN Set pEventFC = pName.BrowseName + "_Events" '+++ Add the layer extension (this is done so that when you edit '+++ the layer's Source properties and click the Set Data Source '+++ button..RouteIDFieldName = "BUS_ID" ..Open '+++ Create the layer and add it to the current map Dim pActive As IActiveView Set pFLayer = New FeatureLayer Set pFLayer.. '+++ The route locator Dim pName As IName Dim pRMLName As IRouteLocatorName Set pDS = pRouteFc Set pName = pDS.RouteIDIsUnique = True .ToMeasureFieldName = "TO_M" Set pDS = pEventTable Set pName = pDS.FullName Set pRMLName = New RouteMeasureLocatorName With pRMLName Set .FullName Dim pRESN As IRouteEventSourceName Set pRESN = New RouteEventSourceName With pRESN Set .EventRouteIDFieldName = "BUS_ID" End With Set pRMLnProp = pRtProp pRMLnProp.FromMeasureFieldName = "FR_M" pRMLnProp. the Add Route Events Dialog appears Dim pLayerExt As ILayerExtensions Dim pRESPageExt As New RouteEventsSourcePageExtension Set pLayerExt = pFLayer 8 .RouteLocatorName = pRMLName End With '+++ By opening a route event source name object..EventTableName = pName Set . you have a 'dynamic' '+++ feature class .FeatureClass = pEventFC pFLayer.RouteFeatureClassName = pName .Name = pDS.EventMeasureUnit = esriUnknownUnits .RouteWhereClause = "" End With '+++ Create the route event properties Dim pRtProp As IRouteEventProperties Dim pRMLnProp As IRouteMeasureLineProperties Set pRtProp = New RouteMeasureLineProperties With pRtProp .'+++ Create the route event source .RouteMeasureUnit = esriUnknownUnits '.EventProperties = pRMLnProp Set .

sDesc End Sub 9 .Description sSrc = Err. sSrc.Raise lNum.pLayerExt.ActiveView pActive.AddLayer pFLayer Set pActive = pMxDoc. sSrc As String lNum = Err.Source Err. sDesc As String.Number sDesc = Err.Refresh Exit Sub eh: Dim lNum As Long.AddExtension pRESPageExt pMap.