Danh mục tài liệu

Vẽ kỹ thuật với Autocad 2006 P2

Số trang: 10      Loại file: pdf      Dung lượng: 429.37 KB      Lượt xem: 11      Lượt tải: 0    
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

In this example, use a question mark (?) to ask the VBAIDE to tell you a variable’s value. To set a variable’s value in the Immediate window, type the statement just as you would in the Code window. Figure 1-10 shows how it’s done.
Nội dung trích xuất từ tài liệu:
Vẽ kỹ thuật với Autocad 2006 P28 CHAPTER 1 ■ THE VBA INTEGRATED DEVELOPMENT ENVIRONMENT (VBAIDE) In this example, use a question mark (?) to ask the VBAIDE to tell you a variable’s value. To set a variable’s value in the Immediate window, type the statement just as you would in the Code window. Figure 1-10 shows how it’s done. Figure 1-10. Setting a variable in Immediate window ■Note You can’t declare variables in the Immediate window. Also, a variable’s scope and valid VBA commands in the Immediate window are equally important. Chapter 2 covers variable scope. The Options Dialog Box The Options dialog box, shown in Figure 1-11, lets you customize the IDE’s look and feel, in- cluding syntax color scheme, source code font, and tab spacing. To open the Options dialog box, choose Tools ➤ Options. This section covers the most commonly used options. Figure 1-11. The Options dialog box For most developers, the default settings are fine. However, you should consider changing two settings on the Editor tab: Auto Syntax Check and Require Variable Declaration. When the Auto Syntax Check option is checked, syntax errors in your code generate an error message similar to Figure 1-12. CHAPTER 1 ■ THE VBA INTEGRATED DEVELOPMENT ENVIRONMENT (VBAIDE) 9 As you begin to develop more complex applications andreuse lines of code from other places in your application, theseerror messages will become a nuisance. Any time you move thecursor off the offending line of code, you get one of these errormessages. But if you uncheck Auto Syntax Check, the VBAIDEnotifies you of errors by changing the color of the offendingline of code to red. Figure 1-12. The error- The Require Variable Declaration option is unchecked by message dialog boxdefault, meaning that the VBAIDE does not require that youproperly declare your variables before you use them. This isn’tmuch of a concern when you write a simple macro, but when you start developing larger andmore complex applications, you’ll find this option indispensable. Checking this option forcesyou to think about each variable and its data type. When you check this option, the VBAIDEadds a line of code to the start of each module, as shown in Figure 1-13.Figure 1-13. An example of the checked Require Variable Declaration After you check this option, exit and restart AutoCAD to make it take effect. By declaring variables to be a specific data type, you save memory resources. Undeclaredvariables are, by default, assigned the variant data type. This data type uses the most memoryand could lead to memory resource problems when users run your application. As a rule ofthumb, always declare each variable you use in your application, and choose the data type thatuses the least possible memory. Chapter 2 discusses data types and memory in more detail.Managing ProjectsManaging your code components is critical to successfully developing applications. Thissection discusses adding components to your project, saving your project, and loading andexecuting an application.Project StructureA VBA project contains several different types of files, including the following: • UserForm module • Standard module • Class module • Reference .dvb file10 CHAPTER 1 ■ THE VBA INTEGRATED DEVELOPMENT ENVIRONMENT (VBAIDE) UserForm Module UserForm modules (files with a .frm extension) contain a text description of your form, controls placement, and property settings. They also include UserForm-level declarations of constants, variables, and procedures; and event procedures. Standard Module Standard modules (files with a .bas extension) contain module-level declarations of programmer- defined data types, constants, variables, and public procedures. A standard module typically contains routines that don’t fit nicely into a class definition. Class Module Use class modules (files with a .cls extension) to create your own objects, including methods, properties, and events. Class modules are similar to UserForm modules except that they have a visible user interface. Class modules are very versatile and vital to VBA and AutoCAD. As you progress through this book, you’ll see that classes and objects are everywhere. Reference .dvb File You can reference the code of another .dvb file in your current project. This feature lets you easily reuse code among several projects. You can’t create a circular reference, which is a refer- ence to one project and a reference in that project to the current project. If you accidentally create a circular reference, AutoCAD tells you of the error. You have to undo the reference before you can continue. Creating, Opening, and Saving Projects To extract, embed, create, save, load, and unload VBA projects, open the VBA Manager dialog box, shown in Figure 1-14. To open it, either type VBAMAN at the AutoCAD command prompt or choose Tools ➤ Macros ➤ VBA Manager. You must explicitly load all .dvb projects. AutoCAD loads embedded projects automatically when the drawing containing them is opened, depending upon how you configure AutoCAD’s security options. Clicking New creates a new project in the VBAIDE that you can access by clicking the Visual Basic Editor button. To load an existing project, click the Load button. The Open VBA Project dialog box in Figure 1-15 appears, letting you choose the project to load. ■ Embedding VBA macros within drawings is fine for drawings that remain within your organization. Tip Avoid embedding macros when you’ll deliver the drawings to outside users or customers as it imposes a securit ...