Understanding automation : Which automation environments are supported? : What is VBA? |
Microsoft Visual Basic for Applications (VBA) is a built-in programming environment that can be used to automate repetitive functions and create intelligent solutions in the software that supports it. VBA is a subset of the Microsoft Visual Basic (VB) object-driven programming environment. Usually, VBA is integrated into another application to customize functionality within that application.
VBA is both a language and an editor. The VBA language cannot be used without its editor, and the VBA editor is the only utility in which VBA code can be edited or VBA programs can be run.
The VBA language is an event-driven programming language. In other words, it is used to write code that produces a response to an action, such as clicking a button or choosing an option from a list box. When the action occurs, the appropriate event is called, and the code for that event is executed. Events can be simple or complex. For instance, you can code a single line that displays a message box or write an entire procedure that interacts with a database.
• |
Most of the code examples provided in this documentation are written in VBA.
|
The VBA editor called the Macro Editor (formerly the Visual Basic Editor) in CorelDRAW is an integrated development environment (IDE) that lets you manipulate the objects that are exposed by the object model of the application. To help you code macros, the Macro Editor provides context-sensitive Help for all the object-model elements that are available to you.
VBA is an in-process automation controller. In other words, VBA can be used to control software features that can be automated, and VBA runs efficiently by bypassing the interprocess synchronization mechanisms. However, the automation that the in-process VBA can access can also be accessed by the following:
• |
external out-of-process automation controllers (OLE clients)
|
• |
applications that are developed in programming languages (such as VB, Visual C++, Windows Script Host,
and C++) that can be used to develop OLE clients
|
• |
the VBA engines of other applications
|
VBA provides a set of tools for customizing the graphical user interface of the software. These tools let you process and present data efficiently and effectively. Advantages of using VBA include the following:
• |
familiarity of the VB language
|
• |
rapid application development (RAD) IDE
|
• |
fast run-time performance of the resulting integrated solutions
|
• |
extensible forms package that supports ActiveX controls for creating user interfaces
|
• |
access to the full Windows application programming interface (API) and the underlying file system
|
• |
connectivity to corporate data
|
• |
integration with other software that is based on component object models (COMs)
|
VBA lets you customize an application to suit your needs, or even integrate it with another VBA-enabled application by referencing the object-model components of the second application. Although VBA was developed by Microsoft and is built into almost all its desktop applications (including Microsoft Office), Microsoft licenses the technology to other companies (including Corel Corporation; Autodesk, Inc., in AutoCAD; and IntelliCAD Technology Consortium, in IntelliCAD). Software products that support VBA can typically be used to control each other, and they can even be used to control some software products that do not support VBA. Consequently, you can use VBA to build solutions in CorelDRAW that access a wide variety of other software products: databases, word processors, XML editors, and more.
• |
For a complete list of applications that support VBA, see the Microsoft website.
|
The VB programming system is an advanced set of programming tools that provides advanced functionality and components for the Windows operating system and other Windows-based programs. For example, unlike VBA or VBScript, VB lets you create application extensions (DLL files) and stand-alone executable programs (EXE files). (The programs that you create with VBA must run inside the host application.)
VB is a visualversion of the BASIC programming language that is, it provides visual cues within the editor. As a result, VB is an easy language to learn. In addition, Microsoft has greatly enhanced the original BASIC language, so that VB is both powerful and fast (although not as powerful as Java or C++, nor as fast as C).
VBA is a subset of the VB programming language, and it uses the programming structure of VB to manipulate the object-model elements that are exposed by an application. The manipulation of these objects results in small packets of code procedures within the application. These code procedures and resulting projects are called add-ins.
VBScript (sometimes referred to as Microsoft Visual Basic, Scripting Edition) is also a subset of the VB programming language. VBScript is a Web-based HTML document scripting language.
VBA is similar to Java and JavaScript in that it is a high-level, procedural programming language with full garbage collection and very little memory-pointer support. (For more information, see Allocating memory.) In addition, code that is developed in VBA, much like code developed in Java and JavaScript, supports on-demand compilation and can be executed without being compiled.
VBA is also similar to JavaScript in that it cannot be executed as a stand-alone application. JavaScript is embedded within Web pages as a mechanism for manipulating the document object model (DOM) of the Web browser. Likewise, VBA programs are executed inside a host environment (such as CorelDRAW) to manipulate the object model of the host.
Most VBA applications can be compiled to p-code to make them run more quickly, although the difference is hardly noticeable because of the sophistication of todays computer hardware. Similar compilation is possible with Java, but not with JavaScript.
Finally, whereas VBA uses a single equals sign ( =
) for both comparison and assignment, Java and JavaScript use a single equals sign ( =
) for assignment and two equals signs ( ==
) for Boolean comparison. (For more information, see Using Boolean comparison and assignment.)
Like C and C++, VB uses functions. In VB, functions can be used to return a value, but subroutines cannot be used in that way. However, functions are used in C and C++, regardless of whether you want to return a value. (For more information, see Using functions and subroutines.)
VBA allocates and frees memory transparently. In C and C++, however, the developer is responsible for most memory management. As a result, using strings in VBA is even simpler than using the CString class in C++.
Finally, whereas VBA uses a single equals sign ( =
) for both comparison and assignment, C and C++ use a single equals sign ( =
) for assignment and two equals signs ( ==
) for Boolean comparison. (For more information,see Using Boolean comparison and assignment.)
Windows Script Host (WSH) is an out-of-process automation controller that lets you do occasional scripting and automation of Windows tasks and can be used to control software. Although WSH is a useful addition to the Windows operating system, WSH scripts tend to be slow because they must run out of process, and they cannot be compiled (and must be interpreted as they are executed).
WSH is a host for a number of scripting languages, each of which has its own syntax. However, the standard language that WSH uses is a macro language that resembles VB, so for standard scripts, the syntax is the same as in VBA.
Copyright 2013 Corel Corporation. All rights reserved.