Sunday, January 24, 2010

Getting Started

INSTALLING AND STARTING VISUAL BASIC FOR APPLICATIONS
As with most Windows-based programs, the installation and startup process for Vi- sual Basic is quite simple. In fact, if you are using VBA, the installation and startup process is accomplished almost automatically when you install and start AutoCAD.


Note: The following installation and startup process is for VBA. For stand-alone VB
installation procedures, see the boxed article on page 18.

INSTALLING VISUAL BASIC FOR APPLICATIONS
To install AutoCAD VBA, all you have to do is make sure you include the VBA module when you install AutoCAD. If you select the Full or Typical option as shown in Figure 2–1, VBA will be installed automatically. The Compact option will not install VBA. If you select the Custom option, you will need to make sure VBA is one of the options selected for installation.


INSTALLING AND STARTING STAND-ALONE VISUAL BASIC
The stand-alone editions of Visual Basic are installed like most other Windows programs on the market today. The software is usually provided on a CD-ROM, and it contains an automated Setup program.

1. With Windows running, insert the CD in the appropriate drive.

2. Click the Start button on the Taskbar. Windows will open the Start menu.

3. Click Run. Windows will open the Run dialog box.

4. Type or select the Setup program and click the OK button.

5. Follow the on-screen directions to complete the installation.

You can run Setup as many times as necessary, meaning you can later change installation options selected during your first installation. Once installation is complete, stand-alone Visual Basic is installed in its own program group.

The Setup program automatically adds Visual Basic to the Programs menu of the Windows Start button. To start Visual Basic, simply click the Start button, highlight Programs, and select the version of Visual Basic installed on your com-
puter. As with other Windows programs, you can also start Visual Basic in several
other ways, such as by using Windows Explorer to find the directory in which
Visual Basic was installed and double-clicking on the EXE file.


STARTING VISUAL BASIC FOR APPLICATIONS
To start AutoCAD VBA, click the Tools menu in AutoCAD and select Macro. You will see the pull-down menu, shown previously in Figure 1–2. From the pull-down menu, select Visual Basic Editor. Alternately, you can type “VBAIDE” in the com- mand line and press Enter. Both methods will open the Visual Basic Interactive Development Environment (IDE), as shown in Figure 2–2. The IDE can be thought
of as the main window where you develop VB programs. It is also sometimes referred
to as the Integrated Development Environment, although some programming pur- ists claim that VBA is not a truly integrated environment like stand-alone VB. Don’t get caught up in the semantics here. The VB Editor in AutoCAD can gener- ally be referred to as the IDE.



EXPLORING THE VISUAL BASIC ENVIRONMENT

ELEMENTS OF THE IDE
The IDE initially consists of five main components: the title bar, the menu bar, the toolbar, the Project Explorer, and the Properties window. Two additional components, the UserForm window and the Toolbox, are displayed when you insert a UserForm to develop a user interface for your program. When you are ready to type code, you display and work with the Code window. A brief description of each component follows.

The Title Bar
The title bar, located at the top of the screen, displays the words “Microsoft Visual Basic,” along with the name of the project you are working on. Initially, the name of the project will be displayed as “Global1.” When you have created and named a project, which we will do later in this chapter, the name of the project will be dis- played in the title bar.

The Menu Bar
The menu bar, located immediately beneath the title bar, provides pull-down menus with some of the key tools you will need to develop VB programs. Many of these menus, like File, Edit, and View, are similar to those of other Windows applications; others, like the Debug and Run menus, are unique to the VB environment. The stand-alone VB menu is similar to the VBA menu, with a couple of minor excep- tions not worth discussing right now. You will learn more about VB and VBA menu commands throughout this book.

The Toolbar
The toolbar, located immediately beneath the menu bar, provides shortcuts to fre- quently used menu commands. Each tool button is represented by a graphical sym- bol, or icon. For example, the button depicting a diskette provides a quick way to save
a file, an alternative to clicking on the File menu and selecting Save. When you
place the cursor over one of the buttons, a brief description, or tool tip, is displayed adjacent to the button. A summary of the toolbar buttons is shown in Figure 2–3.


The toolbar shown in Figure 2–3 is also referred to as the standard toolbar, since VBA also provides additional toolbars for other tasks like editing and debugging. These toolbars are turned off by default, but you can activate them by clicking on the View menu and selecting Toolbars.

The Project Explorer
The VB programs or applications you develop are referred to as projects. To help you manage projects, the Project Explorer displays a listing of the items associated with the project currently loaded. These items—objects, forms, and modules—constitute the building blocks of your program, and they will be discussed in more detail later. If you used previous versions of VB or VBA, the Project Explorer may have been referred to as the Project window. It can still be thought of as a window, and it generally appears in the upper left portion of the screen.

The Properties Window
The Properties window, located below the Project Explorer, lists the properties
for selected objects and their current settings. For the time being, consider
properties as object characteristics, like size, color, and location. When you
first open the IDE, the Properties window will be empty because you have not
yet created any objects. You are essentially starting with a blank page, but that
will change shortly!


The UserForm Window and the Toolbox
We now need to add one key piece to the screen before we can develop a VBA program. The UserForm is the display area, or form, where you develop the window that users will see when they run the program. It is contained in the UserForm window, which appears when you have inserted a form. Insert a form now by clicking on the Insert menu and selecting UserForm. A blank form will be dis- played along with the Toolbox, which is a palette of controls, or components like command buttons, text boxes, and checkboxes that you can place on your form. The Properties window will also display properties of the UserForm and any ob- jects added to it. The updated display showing the UserForm window and Toolbox appears in Figure 2–4.

Note: In stand-alone Visual Basic, the UserForm window is simply referred to as the
Form window, and it is displayed automatically at startup.



The Code Window
At this point you may be wondering where to see the program code—the statements that tell the program what to do. Code is contained in the Code window, which is initially hidden. To display the Code window at any time, click the View menu and select Code, or simply press F7 KEY. Another way to display the Code window is to double-click a control in a UserForm, as you will see in the exercise that follows.

No comments:

Post a Comment