Part 2: Adding the VBA Utility to ArcMap

Tutorial Navigation | Previous: Part 1: Overview of the VBA Utility | Next: Part 3: Setting Up Visual Studio

This step demonstrates the process of adding a VBA utility to ArcMap.

Resources

Files

The related code and form files can be downloaded here: VBA Source and Sample Data (141 KB)
Contents (click to expand for more information):

  • VBA_Tutorial_01_v9.mxd (+/-)
  • VBA_Tutorial_01_VBAembed_v9.mxd (+/-)
  • VBA_Tutorial_01_VBAembed_v10.mxd (+/-)
  • sample_data.mdb (+/-)
  • vba_source\form_OffsetFeatures (.frm, .frx) (+/-)
  • vba_source\offset_features.bmp (+/-)
  • vba_source\OffsetFeatures.txt (+/-)

Video Demonstrations

The following videos are also linked from the relevant sections below. (opens in a new browser window or tab)

Adding the VBA code and form to ArcMap (duration 0:58)
Adding a new toolbar in ArcMap (duration 0:30)
Adding a button control to the toolbar in ArcMap (duration 1:04)

Use the Embedded VBA Code and Forms

In ArcMap 10.0, simply open up the map document that has the VBA Utility embedded in the MXD (VBA_Tutorial_01_VBAembed_v10.mxd). Before doing so, ensure that the VBA environment for ArcMap has been installed and activated. Activation requires requesting an authorization code from ESRI and entering it into the ArcGIS Adminstrator application. ArcMap 10 does not support placing VBA code in a Normal.mxt to make it available to all documents.
[NOTE: ArcMap 10.1 will not support VBA at all. (ESRI Deprecation Document)]

In ArcMap 9.x, the VBA environment is installed by default. Either open up the map document that has the VBA Utility embedded in the MXD (VBA_Tutorial_01_VBAembed_v9.mxd), or follow the instructions below on how to manually add the utililty to the ArcMap 9.x “Normal.mxt” so that it is available to any map document. In that case use the map document without the embedded VBA (VBA_Tutorial_01_v9.mxd).

Manually Add the Code, Form, Toolbar and Button in ArcMap 9.x

Add the VBA Code and Form to ArcMap

Video Demonstration: Adding the VBA code and form to ArcMap (duration 0:58)

Open the provided VBA_Tutorial_01_v9.mxd file.

Open the VBA editor

ArcMap’s VBA editor can be opened by either pressing ALT+F11, or selecting it on the Tools | Macros menu.

Import the VBA form file

Right-click on Normal (Normal.mxt) in the VBA editor’s project inspector and select Import File… on the context menu.

Browse to the provided form file (form_OffsetFeatures.frm), where you unzipped it on your system.

After importing the form, a new Forms folder will be added in the project inspector. Expand that folder and double-click on the new form_OffsetFeatures item to view the form in the designer.

Copy and paste the VBA source code

Expand the ArcMap Objects folder and double-click on the ThisDocument item to open the code editor. Locate the OffsetFeatures.txt file which contains the source code and open it in a simple text editor such as Notepad. Select all the text (CTRL+A) and copy it to the clipboard (CTRL+C), then close the text editor. Click in the code area in the VBA code editor and paste the code (CTRL+V).

Add a New Empty Toolbar

Video Demonstration: Adding a new toolbar in ArcMap (duration 0:30)

Close the VBA Editor. Select Customize… from ArcMap’s Tools menu. On the Customize dialog, select the Toolbars tab. Click the New… button. Enter a name for the toolbar and confirm whether Save in is set to your preference. Click OK and the new toolbar will appear floating over the ArcMap application. Drag the toolbar to an empty space on a tool panel to dock it.

Add a Button Control to the Toolbar in ArcMap

Video Demonstration: Adding a button control to the toolbar in ArcMap (duration 1:04)

Add a new button linked to the VBA code

Select the Commands tab page. Ensure the Save In setting is at your preference. Scroll to the very bottom of the Categories list and select the [UIControls] item. Click the New UIControl… button and select the UIButtonControl item on the subsequent dialog and click OK. This will enter a new item in the Commands list with a default name. Click on the name once to select it, and again (after a delay) to place the text in edit mode. Change the part of the name after the dot to OffsetFeatures. It must exactly match the sub and function names used in the VBA code (i.e. OffsetFeatures_click() ) for the button to be linked to the code. Press Enter after editing the name to save the change, but do not close the Customize dialog.

Add the button to the toolbar and set its icon

Drag the small button icon to the left of the button control’s name in the Commands list to the empty toolbar you already created. To change the button’s icon, right-click on the button on the toolbar to open its context menu. Hover over the Change Button Image item and then click Browse… and locate the offset_features.bmp file provided. The Customize dialog must remain open during this operation.

Close the Customize dialog.

That’s it! You have hooked the button to the code. To test it, start an edit session and select the Hwy_94_Overlaps layer in ArcMap. Click the new Stagger Offsets Features button, enter 350 in the East-West Route box, and click OK.

Tutorial Navigation | Previous: Part 1: Overview of the VBA Utility | Next: Part 3: Setting Up Visual Studio

  1. #1 by Sara on May 7, 2012 - 3:56 pm

    Jeff,

    I would like to create an ultra simple macro to move points by a specified offset; not staggered, just move all of them a set delta X,Y distance. Would I be able to modify your VBA stagger offset code to move the entire group in one step?

    Cheers,
    Sara

  2. #2 by qtools_gis on May 8, 2012 - 10:10 am

    Hi Sara,

    Rather than modifying the stagger offset VBA code, I recommend that you start with the original ESRI code it was based upon, since it is closer to the action you wish to perform.

    Please see the ESRI Move Features example on EDN.

    Good luck!
    Jeff

  3. #3 by Muhammad Azam on March 3, 2015 - 9:46 pm

    This method is for 9.3 users but how to add VBA Script in ArcMap 10.

  4. #4 by qtools_gis on March 6, 2015 - 10:16 am

    Hello Muhammad,

    In ArcMap 10 you must first install the VBA environment. Prior to version v10 it was installed automatically and VBA “just worked.” With the release of v10, ESRI started a push to move users from VBA to ArcMap Add-ins. Here are some articles that may help you.

    Installing VBA environment into ArcGIS 10:
    http://support.esri.com/en/knowledgebase/techarticles/detail/40201

    Transition from VBA to ArcMap Add-ins using VB.NET:
    http://www.ian-ko.com/free/EC10/CustomizationEnvironment10.htm

    I hope this helps you.
    -Jeff

  5. #5 by Amandeep Kalra on November 7, 2015 - 5:33 am

    Hello,
    I have to add a customized toolbar for the geodatabase i have created in arc gis 9.3 so that a window pops up showing the database on clicking the toolbar menu rather than using the identify tool and showing all the details. As visual basic editor is not installed internally in arc gis 9.3, i would like ou to help me with the steps on how to download it and proceed with my project. Thanking you in anticipation.

(will not be published)