Tài liệu Vẽ kỹ thuật với Autocad 2006 P2 doc - Pdf 10

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.
■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.
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)8
Figure 1-10. Setting a variable in Immediate window
Figure 1-11. The Options dialog box
5793c01_final.qxd 8/22/05 2:41 AM Page 8
As you begin to develop more complex applications and
reuse lines of code from other places in your application, these
error messages will become a nuisance. Any time you move the
cursor off the offending line of code, you get one of these error
messages. But if you uncheck Auto Syntax Check, the VBAIDE
notifies you of errors by changing the color of the offending
line of code to red.
The Require Variable Declaration option is unchecked by
default, meaning that the VBAIDE does not require that you
properly declare your variables before you use them. This isn’t
much of a concern when you write a simple macro, but when you start developing larger and
more complex applications, you’ll find this option indispensable. Checking this option forces
you to think about each variable and its data type. When you check this option, the VBAIDE

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.
■Tip Embedding VBA macros within drawings is fine for drawings that remain within your organization.
Avoid embedding macros when you’ll deliver the drawings to outside users or customers as it imposes
a security risk on their part to trust your macros in their environment.
This dialog box is similar to the standard File Open dialog box in Windows. Similar to cre-
ating a new project, when you choose the .dvb project you want, click the Visual Basic Editor
button to start working on your project.
CHAPTER 1 ■ THE VBA INTEGRATED DEVELOPMENT ENVIRONMENT (VBAIDE)10
5793c01_final.qxd 8/22/05 2:41 AM Page 10
There are two other ways to create and load DVB project files:

removal). You can also highlight the module name, right-click to invoke the pop-up menu in
Figure 1-18, and then choose Remove.
Notice that this menu also includes the Export File option.
CHAPTER 1 ■ THE VBA INTEGRATED DEVELOPMENT ENVIRONMENT (VBAIDE)12
Figure 1-16. Save As dialog box
5793c01_final.qxd 8/22/05 2:41 AM Page 12
Adding ActiveX Controls and Code Components
When you start a project and add a UserForm module, a common Toolbox appears. It contains a
standard collection of ActiveX controls called intrinsic controls. Chapter 3 covers intrinsic con-
trols in more detail. If you want to insert an ActiveX control that is not in the Toolbox, choose
Tools
➤ Additional Controls or right-click in the Toolbox window and choose Additional Con-
trols. The Additional Controls dialog box shown in Figure 1-19 appears.
This dialog box lists all the ActiveX controls that are properly registered on your machine.
However, if you want to use a particular ActiveX control, check its End User License Agree-
ment (EULA) to determine whether you have a license to use it in a VBA host application.
CHAPTER 1 ■ THE VBA INTEGRATED DEVELOPMENT ENVIRONMENT (VBAIDE) 13
Figure 1-17. The Import File dialog box
Figure 1-18. The
Remove Module
pop-up menu
Figure 1-19. The Additional Controls dialog box
5793c01_final.qxd 8/22/05 2:41 AM Page 13
Many ActiveX controls installed with Microsoft Visual Basic, including TreeView, ListView,
File, Directory, and Drive, are not licensed for use in a VBA host application. However, Chap-
ter 19 explains some easy ways you can use the Windows application programming interface
(API) to get around this dilemma.
You can add more than ActiveX controls—code components are perhaps even more com-
mon. This is the means by which your application can gain access to other ActiveX automation
applications such as Microsoft Excel, Access, and Word. ActiveX automation is your key to

it loads it into the current session. The following example illustrates how to implement this
feature.
Public Sub Start()
Application.ActiveDocument.SetVariable "OSMODE", 35
End Sub
Place this code in the ThisDrawing module and save the file as acad.dvb. Save this file in
any subdirectory specified in the support file search path. Now each time you start an AutoCAD
session, AutoCAD loads this file. Also, if you include a routine called AcadStartup, AutoCAD exe-
cutes it when it loads acad.dvb.
The example in Figure 1-22 shows how to use the macros that you have stored in the
acad.dvb file.
CHAPTER 1 ■ THE VBA INTEGRATED DEVELOPMENT ENVIRONMENT (VBAIDE) 15
Figure 1-21. The Object Browser dialog box
5793c01_final.qxd 8/22/05 2:41 AM Page 15
Type –vbarun then the name of the macro to run (Start in this instance). In this example,
the value of the system variable OSMODE is set to 35. This is a convenient way to store macros
that will invoke AutoCAD with a particular setup.
Acaddoc.lsp
AutoCAD automatically searches the default search path (a combination of the support/file path
list, the current working folder, and the shortcut’s startup folder) for a file named acaddoc.lsp. If
AutoCAD finds this file, it loads it into the current drawing. Unlike with acad.dvb, which loads
only when you start a new AutoCAD session, AutoCAD loads acaddoc.lsp each time you open or
create a drawing.
■Tip To verify the actual search-path list, go to the command prompt, type
-insert
, enter a meaningless
string of characters such as
sdfsdfsdf
(keep it less than 31 characters, though), and press Enter. AutoCAD
displays a list of folders in which it tried to find your file when it failed.

also loads the VBA project embedded in the drawing. Of all the options to load VBA projects auto-
matically, this is the worst one. It stores the VBA project with the drawing, making your drawing
file that much bigger. If you copy the drawing file to create a new drawing, you also copy its VBA
project. If you want to change the VBA project, you need to change each VBA project in every
drawing file that you created from the original. In addition, delivering drawings with embedded
macros to customers imposes a serious potential security risk on their part. Well, you get the pic-
ture of why this is the worst option to choose!
VBARUN and the Macros Dialog Box
The dialog-box version of the VBARUN command features several options that extend how you
create and execute a VBA macro.
You can execute a VBA macro at the AutoCAD command prompt. To execute a macro
from the AutoCAD command prompt, type –VBARUN, press Enter, and type the macro name,
similar to the example in Figure 1-23.
In this example, AutoCAD would execute the Start macro.
If the macro you wish to execute is unique among all the VBA projects loaded, then just
specify the macro name after the –VBARUN command. However, if you have multiple macros
loaded with the same name, specify the macro to execute using either of these syntaxes:
ProjectName.ModuleName.MacroName
or
ModuleName.MacroName
How far down inside your project and modules your macro is placed, and whether you
have multiple projects loaded determine which syntax is appropriate.
CHAPTER 1 ■ THE VBA INTEGRATED DEVELOPMENT ENVIRONMENT (VBAIDE) 17
Figure 1-23. The AutoCAD command prompt
5793c01_final.qxd 8/22/05 2:41 AM Page 17


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status