Microsoft® Visual Basic® for Applications (VBA) software provides application integration with other VBA-enabled applications.
AutoCAD-based products can be used as an Automation controller for other applications such as Microsoft Word or Excel.
Note: VBA is no longer installed by default. For more information, visit http://www.autodesk.com/vba-download.
VBA sends messages using the AutoCAD ActiveX Automation Interface. AutoCAD VBA permits the Visual Basic environment to run simultaneously with AutoCAD and provides programmatic control of AutoCAD through the ActiveX Automation Interface. This linking of AutoCAD, ActiveX Automation, and VBA provides an extremely powerful interface. It not only controls AutoCAD objects, it also can send data to or retrieve data from other applications.
Before you can edit or run a macro from a VBA project, it must be loaded into AutoCAD first. VBA projects are stored in separate files with the .dvb extension, and are composed of code and class modules, and forms. Since VBA projects are stored in a separate file, a VBA project can open and close different AutoCAD drawings during a session which cannot be done with AutoLISP.
Note: VBA projects are not binary compatible with Visual Basic projects (VBPROJ) files created with Visual Studio.
Fundamental elements of AutoCAD VBA
Three fundamental elements define VBA programming in AutoCAD.
- AutoCAD - Offers a rich set of objects that include AutoCAD entities, data, and commands. AutoCAD programmability is highly desirable. However, you will find that the VBA object-based approach is quite different from that of AutoLISP.
- AutoCAD ActiveX Automation Interface - Establishes messages (communication) with AutoCAD objects. Programming in VBA requires a fundamental understanding of ActiveX Automation.
- VBA - Provides its own set of objects, keywords, and constants that provide program flow, control, debugging, and execution. The Microsoft extensive Help system for VBA is included with AutoCAD VBA.
Advantages of using VBA
The AutoCAD ActiveX/VBA interface provides several advantages over other AutoCAD API environments:
- Speed. Running in-process with VBA, ActiveX applications are faster than AutoLISP applications.
- Ease of use. The programming language and development environment are easy-to-use and integrated with AutoCAD after it has been installed.
- Windows interoperability. ActiveX and VBA are designed to be used with other Windows applications and provide an excellent path for communication of information across applications.
- Rapid prototyping. The rapid interface development of VBA provides the perfect environment for prototyping applications, even if those applications will eventually be developed in another language.
- Programmer base. Programmers commonly know Visual Basic .NET (VB.NET). VBA is similar to VB.NET, and opens up AutoCAD customization and application development to these programmers as well as those who are willing to learn a programming language beyond AutoLISP.