HOME VBA is gone: Who cares? HOME
 

 

VBA was the customization environment in ArcGIS since the release of version 8.0 some 10 years ago. The users of ArcGIS desktop were able to quickly open the VBA Editor embedded in ArcGIS and write code that automates certain tasks or even create custom VBA applications embedded in the ArcGIS projects. Now with the release of ArcGIS 10.0 VBA is already not available for use within the Field Calculator. With the release of ArcGIS 10.1 VBA will be totally gone.

So what are the alternatives?

  • Python

  • .NET

We are not going to discuss here the advantages and disadvantages of each of the above. The main purpose of this article is to outline how to create a development environment similar to VBA with ArcGIS 10 that will allow easy transition to a VBA-less ArcGIS customization and our choice for this is .NET.  The main reasons for this are:

  • We can use all VBA knowledge accumulated during the years
  • We can easy convert existing VBA scripts to VB.NET
  • There is a FREE development environment (Visual Studio .NET express) allowing easy debugging
  • We can have full access to the ArcObjects Development Kit
  • ArcGIS 10 offers a new customization framework - the add-in model which is very well integrated with Visual Studio and simplifies the development and sharing of custom applications.
  • We can make use of existing books and resources written for Visual Basic ("Getting to know ArcObjects", etc.), but can be applied for VB.NET almost with no changes.
  • We will get exposed to a mainstream development environment

To conclude this introduction lets just compare two code snippets illustrating the easiness of converting VBA code to VB.NET

Sample VBA routine The VBA code from the left copied and pasted in Microsoft Visual Studio-VB.NET project (NO CHANGES MADE).
One can see that when pasting VBA code in Microsoft Visual Studio(VS) many of the syntax differences are fixed automatically and the code is almost the same. The only error in this case reported by VS is that it cannot find "ThisDocument" which in ArcGIS VBA Editor refers to the currently active ArcGIS document , which can be easily fixed (see below).

Getting started

  • Download Microsoft Visual Studio 2008 Express. - Note that there is Visual Studio 2010 Express is available, but ArcGIS 10 currently does not support it. So make sure to get version 2008 and install Visual Basic Express.

  • Install ArcGIS SDK for the Microsoft .NET Framework

Creating your development environment

  • Start Visual Basic Express

  • Go to Project ==> Properties ==> Compile ==> Advanced Compile Options and make sure that the Target Framework is set to .NET Framework 3.5

  • Click File ==> New Project

  • In the dialog

  1. Navigate to ArcGIS ==> Desktop Add-Ins and select ArcMap Add-in
  2. Give a name to your project
  3. Click OK
  • On the first page ArcGIS Add-in Wizard fill the name and description

  • On the second page select  Button, give it a name and fill the rest of the fields

  • Click Finish

In the Solution Explorer you will see a VB module added with the name of your button. Double click on it to open the code.

Lets add to the On Click procedure a single message box.
  • Go to File ==> Save All to save your work

  • Now if you click on the Start Debugging Button (or go to Debug ==> Start Debugging) Visual Basic will start ArcMap ( Note that although you cannot set  an external program in the Express edition of Visual Studio, the ArcGIS Add-in Wizard has added ArcMap as a program to start when you debug your project).

  • To add your button to ArcMap go to Customize

In the dialog go to Commands, select the Add-In Controls category, find your button and drag it to any toolbar.

Close the dialog

If you click on the new button, you should get your code executed
.


Now we have something very similar to the VBA environment we are so used to. We need to add a procedure  that will do something useful, and when call the procedure from the button it will execute our code.

Creating a custom procedure

  • We can add our code directly in the On Click event of the button, but to separate the code from the interface we'll create a new VB module. In a very similar way we are used to do this in VBA - right click on the solution ==> Add ==> New Item

  • In the dialog select  Common Items ==> Module. Give name to the module and click OK

  • Double click on the new module to open it for editing

  • Paste an existing VBA procedure in the module

  • Start Debugging ==> In ArcMap select a polyline feature layer ==> Click the button

We can see that some of the interfaces used are not recognized and reported as errors. To fix this we need to add some references to ArcObjects. The easiest way to find out which library needs to be referenced we can go to the ArcObjects developers help and check the interfaces reported as "not defined". 
For IFeature for example the library is ESRI.ArcGIS.Geodatabase, for IPolycurve - ESRI.ArcGIS.Geometry, etc.

To add the needed references, right click on the solution ==> Add Reference.  In the dialog select the .NET tab, then find and select the libraries required
To make our life easier (and typing less) we can import the libraries mentioned above in our module

Now the only error reported by Visual Basic is ThisDocument which in VBA was used to get hold of our current document.

In ArcGIS 10 Add-ins targeting ArcMap we can replace this with
My.ArcMap.Document

Now our procedure is ready to run. We just need to call it from the button we created. To do that we will replace our "Hello World" in the OnClick procedure of the button with

Call generalize()

NOTES:

  • The aim of this article is just to show how easy is to create a VBA like development environment and convert VBA code to VB.NET. 

  • You will find a better and more detailed description of the Add-Ins in the ArcObjects Help for .NET

  • The code samples above can be improved significantly (adding error trapping, explicit type conversion, etc.), but this is not in the scope of the article.

For any comments and enquiries contact: webmaster@ian-ko.com
Esri and all Esri products mentioned are trademarks of Environmental Systems Research Institute, Inc.
Copyright: Ianko Tchoukanski