Show Navigation | Hide Navigation
You are here:
Customizing and developing with ArcGIS > Writing macros using VBA

Creating, editing, and running macros

Release 9.2
Last modified November 9, 2006
E-mail This Topic Printable Version Give Us Feedback

Print all topics in : "Writing macros using VBA"


Related Topics

About creating, editing, and running macros

ArcGIS applications come with Visual Basic for Applications. VBA is not a stand-alone program; it's embedded in the applications. It provides an integrated programming environment, the Visual Basic Editor (VBE), which allows you to write a Visual Basic (VB) macro, then debug and test it right away in, for example, ArcMap. A macro can integrate some or all of VB's functionality, such as using message boxes for input, with the extensive object library that the ArcGIS applications expose. The ESRI Object Library is always available to you in the VBA environment.


Macros and VBA projects

ArcMap, ArcGlobe, and ArcScene use a document/template structure to store maps, globes, and scenes and any customizations to the user interface. Customizations can include changing the location of toolbars, menus, tools, commands and windows, along with using VBA macros to extend the functionality of the desktop applications.

In the VBA development environment, you can add modules, class modules, and user forms to a project using the VBE. You can open the VBE in any of the above-listed applications by clicking Tools on the Main menu, pointing to Macros, then clicking Visual Basic Editor.

opening the Visual Basic Editor from the Tools menu

In ArcMap, ArcGlobe, and ArcScene, each currently loaded document and template has an associated VBA project in the VBE, allowing you to write macros that are specific to a given map, globe, or scene document or to apply macros to all the documents consuming a specific template. The VBA project for the document is called Project followed by the name of the document in brackets. For example, in ArcMap it is named Project (<name of document>.mxd), and the VBA project for the Normal template is called Normal (Normal.mxt). In ArcGlobe, the VBA project for the Normal template is called Normal (Normal.3dt), as shown below.

ArcGlobe Project in VBE

In ArcMap, map documents support more than one template. If an additional template is loaded, its VBA project is called TemplateProject (<name of template>.mxt). This is not supported in ArcGlobe or ArcScene.

ArcMap Project in VBE

ArcCatalog is somewhat different from ArcMap, ArcGlobe, and ArcScene, because it has only a single VBA project for the Normal template, called Normal (Normal.gxt). There is no concept of a document, and you cannot add additional templates.

ArcCatalog Project in VBE

Macros can be stored in any of the VBA projects, depending on where you want the new functionality to be available.


Creating macros

When you create a macro, you're creating a VB Sub procedure. The procedure's name is the name you assign to the macro. You add code to the procedure on a Code window just as you would in VB. When you create a new macro on the Macros dialog box, precede the macro's name with the name of the module in which to store it. You can organize your macros in different modules; each module has its own Code window. To add your macro to a specific module, type the module name before the macro's name, for example, "Department.WorkMacro". If the module doesn't exist, a new module with that name is created for you and added to the VBA project. Similarly, if you provide a name for a new macro but don't specify which module to store it in, a new module, NewMacros, is created. Using modules makes it easier to share your VB code with others; you can export a module to a .bas file from, and import a .bas file to, your VBA project.


How to create, edit, and run macros

Creating a macro

  1. Click the Tools menu, point to Macros, then click Macros.
  2. Click the drop-down arrow of the Macros in combo box, then click the document or template in which you want to create the macro.
  3. Type the name of the macro you want to create in the Macro name text box.
  4. Preceding the name of the macro with a module's name and a dot stores the macro in the specified module. If you don't specify a module name, the application stores the macro in a module named NewMacros.
  5. Press Enter or click Create.
  6. The stub for a Sub procedure for the macro appears in the Code window.
  7. Type the code for the macro between the first line (Sub name()) and last line (End Sub).
  8. Click the VBE File menu and click Save Project.


Editing a macro

  1. Click the Tools menu, point to Macros, then click Macros.
  2. Click the drop-down arrow of the Macros in combo box, then click the document or template in which the macro is saved.
  3. In the list below the Macro name text box, click the name of the macro you want to edit.
  4. Click Edit.
  5. The code that's been written for the macro appears in the Code window.
  6. Edit the code.
  7. Click the VBE File menu and click Save Project.
  8. Click Close to close the VBE.


Running a macro from a Visual Basic module

  1. In the Visual Basic Editor, open the Code window where the macro's code is saved.
  2. In the Code window, click inside the Sub procedure of the macro you want to run.
  3. Click the Run menu and click Run Sub/UserForm.


Running a macro in the Macros dialog box

  1. Click the Tools menu, point to Macros, then click Macros.
  2. Click the Macros in drop-down arrow and click the document or template containing the macro you want to run.
  3. Type the name of the macro you want to run or click its name in the list.
  4. Click Run.


Adding a macro to a toolbar

  1. Click View, point to Toolbars, and check the toolbar to which you want to add a macro.
  2. Click the Tools menu and click Customize.
  3. Click the Commands tab.
  4. Click [Macros] in the Categories list.
  5. Click and drag the macro from the Commands list and drop it on the toolbar.
  6. Click Close.

Tips

  • If you want to view or edit the code for this macro item, right-click its button on the toolbar and click View Source.
  • There is a Visual Basic Editor command you can drag and drop onto a toolbar for quick access to the VBE. To access this, click the Tools menu, click Customize, click the Commands tab, and click the Tools in the Category list to view it in the Commands list.
Please visit the Feedback page to comment or give suggestions on ArcGIS Desktop Help.
Copyright © Environmental Systems Research Institute, Inc.