Such a variable is often known as a local variable. A variable declared in a procedure is not available outside that procedure. Scopes of the Variable Scope of a variable refers to the area of VBA where the variable is available. The Document property of the Application global object returns the pointer to the interface. The Set pmxdocument = Application.Document statement points the pmxdocument to the IMxDocument interface to access the ArcMap document. In this manner they point to an object through the object s interface. Each variable is declared as an ArcObjects interface. Option Explicit Sub MyMacro() Dim pmxdocument As IMxDocument Dim pmap As IMap Dim player As ILayer Set pmxdocument = Application.Document Set pmap = pmxdocument.focusmap Set player = pmap.layer(0) MsgBox player.name End Sub In the preceding example there are three variable declarations. The macro produces an error if there are no layers. The following example shows a macro that displays the name of the first layer in a map. The Application keyword references the Arc- Map program, and ThisDocument points to the current ArcMap document. The two objects can be accessed with the keywords Application and ThisDocument. Using the Global Application Objects VBA offers two objects that are always available to you while running ArcMap. You can also declare multiple variables on a single Dim statement, separating each variable with a comma. Option Explicitģ Referencing Objects with Variables 13 You can declare the variable anywhere in your macro, as long as it is before its first use. You can force the explicit declaration by adding the following line to the beginning of your module. You are strongly urged to always declare variables. Object: Variables of this type point to objects. String: This data type is for storing text. Double: Variables of this type store numerical values with decimal point. Date: Date and time values are stored in these types of variables. Boolean: Variables of this type can be either True or False. The following are other common data types. Dim MyVar As Long In the preceding example MyVar can only accept numeric data types of long integers. You can explicitly indicate the data type a variable can accept, as in the following. Variant variables can accept different types of data. The preceding example of a declaration creates a variable named MyVar as a variant. For example, declaring with the Public keyword makes the variable available to more than one procedure. Other keywords for declaration have special purposes. It declares a variable to be used inside a procedure.
Dim MyVar The Dim keyword is the most used method of declaration. For example, to declare the MyVar variable you use the following statement. To declare a variable, you use one of the following keywords: Dim, Private, Public, or Static. In the case of explicit declaration, you declare the variable before using it. When VBA encounters the variable for the first time, it will declare it. For implicit declaration, just use the variable. However, you can declare variables explicitly or implicitly. 11Ä¢ 12 Chapter 2: The VBA Programming Language VBA requires that you declare variables in advance of their use. Throughout this book you will see many examples of how variables can point to interfaces to access objects in ArcObjects. An object s interface organizes related properties and methods of an object. Instead, it points to one of the object s interfaces. In ArcGIS, your application does not directly reference an object. For example, when an application turns on the visibility of a layer, it needs to reference the Layer object.
Objects are the things you work with when developing ArcGIS applications. Variables can store data types such as text strings and numbers. A variable is an area in memory reserved for storing a piece of information that can be accessed or changed by a program.
These types of information are stored in variables. To accomplish these and other tasks, your VBA program needs information such as the shape file name or page size for layout. Referencing Objects with Variables Your application can guide ArcGIS to execute many types of tasks, such as loading a shape file or preparing a map layout. Even if you are not a programmer, VBA is easy to learn. If you have studied other programming languages, the VBA language will be familiar to you. This chapter presents VBA s basic programming elements, which include variables and control statements. 1 CHAPTER 2 The VBA Programming Language TO BECOME PROFICIENT IN DEVELOPING ArcObjects macros and applications, you need to first become fluent in the VBA programming language.