Creating, editing, and running macros |
|
Release 9.2
Last modified November 9, 2006 |
![]() ![]() ![]() Print all topics in : "Writing macros using VBA" |
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.
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.
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.
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.
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.
Macros can be stored in any of the VBA projects, depending on where you want the new functionality to be available.
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.
Tips |
|