Adding a button that will run custom code
Problem: You have written or found a script that you would like to be able to run by pressing a button on your ArcMap toolbar
There is a world of ways to extend to the basic functionality of ArcGIS, beyond just installing plugins. People have been writing custom code for years in various languages, and making it available in places like ArcScripts and the ESRI Developer Network.
Today I will show you how to take a script from a place like that and attach it to a button in your ArcMap toolbar so it will be available whenever you run the application. I have begun learning to program ArcObjects with Visual Basic for Applications, so I chose a VBA sample as my example code. The code I found will spell check label text using the Microsoft Word spell checker. It is available in the ESRI Developer Network code exchange here.
Note: The ESRI Developer Network is moving to the ArcGIS Resource Center for versions 9.3 and above. Newer sample VBA codes are available through the resource center here: http://resources.esri.com/help/9.3/arcgisdesktop/com/vba_start.htm
Before you worry about the code, you need to create your button. You can add a new button to any toolbar by right clicking on that bar and selecting Customize. If you go to the Commands tab and scroll all the way to the bottom, you will see a [ UI Controls ] option. If you highlight it you will be able to press a button to create a new UI (User Interface) Control.
If you save the control in Normal.mxt, it will will show up in every ArcMap session from now on. You can also save it only in the current .mxd, if you prefer. Select a UIButtonConrol, and click Create. Normal.UIButtonControl1 will now show up in your Commands list box. “Normal” indicates where you are saving the command and “UIButtonControl1″ is the name of the command. If you click on it you will be able to rename it to something more descriptive. I chose Normal.SpellCheck. Now, you can highlight it and drag it to any toolbar.
If you right click on the new button while the Customize dialog is still open, you will be able to change its appearance. You can chose from a selection of images, browse to a custom image, add text with the image, or chose text only. I did text only. Your command name will be used as the button text. At this point, if you pick View Source off the same menu, the Visual Basic Editor will open. A window will appear where you can paste the code for the spell check tool. You can get the code from here:
http://edndoc.esri.com/arcobjects/9.2/CPP_VB6_VBA_VCPP_Doc/COM_Samples_Docs/ArcMap/9be43e79-834e-42a2-96c8-59b0dba3180b.htm
Copy everything between…
Public Sub SpellCheck()
and
End Sub
…but don’t copy the opening or closing line itself. Visual basic has already added those lines for you, formatted correctly to work with the button you created. For instance, mine starts as:
Private Sub SpellCheck_Click()
Now, you will notice in Step 3 of the “How to Use” instructions that the developer tells you to add a reference to the Microsoft Word Object Library. You can add references by going to the Tools menu and selecting “References..”. The following list will come up. Scroll down and check off the appropriate library.
Be sure you save Normal.mxt (File menu or disk button) before exiting the Visual Basic Editor. Now, when you’re back in ArcMap, create some text labels. Then select one of them and press your new button. If you have any misspelled words in the selected label, a spell checker will pop up that should look quite familiar! Any corrections you make will be carried over to the label.
If you would like to learn more about ArcObjects and VBA, Getting to Know ArcObjects by Robert Burke seems great so far. It’s what I’m reading now.
Tags: ArcObjects, automation, spell check, VBA
October 12th, 2009 at 9:01 am
As you may know, VBA and VB6 will fade out from ArcGIS with newer versions. Read about it here: http://blogs.esri.com/Dev/blogs/arcobjectsdevelopment/archive/2009/03/30/VBA-and-VB6_3A00_-The-Road-Ahead.aspx