The Office/Visual Basic Relationship
Visual Basic for Applications is a combination of an integrated programming
environment (the Visual Basic Editor) and the Visual Basic programming language. This
combination allows you to easily design and develop Visual Basic programs. The term
"for Applications" refers to the fact that the Visual Basic programming
language and the development tools in the Visual Basic Editor are seamlessly integrated
with all Office applications so that you can develop custom functionality and feature
solutions using these applications.
The Visual Basic Editor
Each document, workbook, presentation, or database you open in Word, Excel,
PowerPoint, or Access respectively has an associated Visual Basic for Applications
project. When you open a workbook in Excel, for example, an associated Visual Basic
project is listed in the Project Explorer window of the Visual Basic Editor. To write
Visual Basic code in the Excel workbook's Visual Basic for Applications project,
you must display the Visual Basic Editor. One way to display it is to point to Macro on
the Tools menu, and then click Visual Basic Editor on the submenu.
All the applications in Office provide the same integrated development
environment. The Visual Basic Editor contains all the programming tools you need to
write Visual Basic code and create custom solutions. For example, you can switch to the
Visual Basic Editor window from PowerPoint the same way you do from Excel (from the
Tools menu, point to Macro, and then click
Visual Basic Editor).
Although the Editor is a separate window in all Office applications, it looks and
functions identically in each one. Thus, you can potentially have three Visual Basic
Editor windows open at one time, each one associated with a separate application. When
you close a given application, its associated Visual Basic Editor window closes
automatically.
Examining the Elements of the Visual Basic Editor
The Visual Basic Editor provides a number of advanced programming and development
tools that were once found only in development programs like Microsoft Visual C++.
- Project Explorer This window displays a hierarchical list of the
projects and all of the items contained in and referenced by each of them. When you
open a document in Word, for example, a Visual Basic for Applications project is
associated with it in the Project Explorer. The items in a Visual Basic for
Applications project can be any number of code modules or UserForms.
- Properties Window This displays an alphabetical or categorized
property list of an ActiveX control in a UserForm, a UserForm itself, or a code
module. An item's list of properties is on the left side of the window and its list
of corresponding values is on the right.
- Object Browser Think of this as a map for navigating through the
objects, methods, properties, and events provided by an ActiveX control or an
application such as Word, Excel, PowerPoint, Access, or Outlook. As you'll learn
later in this chapter, the Object Browser will be an invaluable tool for determining
how you can program a specific object provided by Office.
- Code Module This is where you write all of your Visual Basic code.
(You see the code in a code module through the Code window.) There are three types of
code modules: standard, class, and form; each type serves a specific purpose. Code
modules allow you to group together code with common functionality.
- UserForm Window This window contains a UserForm that allows you to
create custom dialog boxes for use in your Visual Basic for Applications programs. With
a UserForm (and the ActiveX controls in the Toolbox), you can re-create any
dialog box you've interacted with in Office and add your own customizations. You
can also create your own dialog boxes to suit the needs of your Visual Basic for
Applications program.
- Toolbox Listed here you'll find a set of ActiveX
controls. Like the Control Toolbox in the Word, Excel, and PowerPoint windows, controls
in the Visual Basic Toolbox can be dragged and dropped. However, in the Visual Basic
Editor, you can drag and drop controls only onto a UserForm.
- Immediate Window Here you can enter and execute one line of Visual
Basic code and immediately see its results. You commonly use the Immediate window when
you debug Visual Basic code.