Microsoft KB Archive/161609

= XL97: Changes to Microsoft Visual Basic for Applications =

Article ID: 161609

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft Excel 97 Standard Edition

-



This article was previously published under Q161609



SUMMARY
The user interface for Microsoft Visual Basic for Applications in Microsoft Excel 97 has changed significantly from the interface for earlier versions of Microsoft Excel. This article describes the changes in the Visual Basic for Applications environment in Microsoft Excel 97.



MORE INFORMATION
The components that make up Microsoft Visual Basic for Applications are the Visual Basic Editor, the forms interface, and the Visual Basic for Applications language.

The Microsoft Visual Basic Editor
The Microsoft Visual Basic Editor (VBE) uses the familiar programming interface of Microsoft Visual Basic 4.0 as a base for creating and editing code. Within the VBE, there are five major elements: Project Explorer, Property Window, Code Window, Object Browser, and Debugging Tools.

 Project Explorer

Code created for a Microsoft Excel workbook is associated to that workbook through a project. The Project Explorer is a navigational and management tool which displays a hierarchical list of each open project. It also shows each of the components associated with those projects, such as worksheets, forms, class modules, and references.

 Property Window

The Property Window displays properties for the active object (UserForm or control). It provides a visual way to set or modify properties. In earlier versions of Microsoft Excel, most properties could only be set programmatically. In Microsoft Excel 97, many of these properties can now be set through the Properties window for an object. In the Property window, properties may be listed alphabetically or by category.

 Code Window Module sheets are no longer available for insertion into a workbook. Instead, a Code window is provided for each workbook, for individual worksheets, and for general modules, class modules, and forms. The Code window includes two drop-down boxes: one (on the left) to identify the Object with which the code is associated, and one (on the right) to identify the Procedure.

To activate a Code window, use one of the following methods:

 Double-click the object for which you want to write or edit code. -or-

 -or-Click the object once to select it, and then click Code on the View menu or press F7. -or-

 -or- Using the right mouse button, click the object, and then click View Code on the shortcut menu.</ul>

For backward compatibility, Microsoft Excel 4.0 Macro sheets can still be inserted and will run normally. Microsoft Excel 5.x and 7.x module sheets are hidden and the code on them is transferred to code modules in Visual Basic for Applications. This code will also run normally.</li> Object Browser The Object Browser is similar to the Object Browser in earlier versions of Microsoft Excel with additional functionality including:

<ul> Differentiates between built-in properties, methods, and user-defined procedures.</li> Indicates which items are globally accessible.</li> Shows function return types, parameter names and types, and user defined types and constants.</li> Has a modeless window so that other activities can be carried out while the browser is displayed.</li> Allows for viewing of all available type libraries.</li> Includes a Find feature to search for objects across libraries.</li></ul> </li> Debugging Tools The debugging tools in VBE include the familiar Watch window and Immediate window in addition to:

<ul> An auto variables window which allows you to automatically see the values of all variables and expressions within a few lines of the current line of execution.</li> A local variables window which includes a call stack browser which allows you to jump to variable and procedure definitions and references.</li> Margin Indicators to visually indicate current lines of execution.</li> Ability to tile the debug window.</li></ul> </li></ul>

One final change is that VBE operates in either Run Mode or Design Mode. Run mode is the default mode when a project is opened and is transparent to the user. Design Mode must be entered explicitly by clicking on the Design Mode button or clicking the Design Mode command on the Run menu. The main benefit of Design Mode is that it allows programmers to stop event handling routines from running automatically.

The Forms Interface
Earlier versions of Microsoft Excel used Dialogs on Dialog sheets to create custom forms. In Microsoft Excel 97, Dialog sheets are still available for backward computability. UserForms are now available for custom forms and can be used in Microsoft Excel 97, Microsoft Word 97, and Microsoft PowerPoint 97.

The advantages of UserForms are that they:


 * Take less time to load, save, and redraw controls.
 * Support data binding to cells and ranges.
 * Support printing and zooming.
 * Include alignment and layout tools, drag-and-drop operations of controls and in-place editing of captions for advanced form design.
 * Support multi-page controls for tab creation.

The Forms toolbar can be customized to display all registered OLE Custom Controls (OCX) in the system. The Forms toolbar can also have additional pages, controls can be moved from page to page, the bitmaps for the controls can be edited, and the tooltips can easily be changed.

Dragging a control from the form back to the toolbar creates a template of that control on the toolbar. For more information about creating a template for a control on the Forms toolbar, please see the following article in the Microsoft Knowledge Base:

160473 XL97: How to Create a Template for a Toolbox Control

The Microsoft Visual Basic for Applications Programming Language
<ul> The Language

Microsoft Visual Basic for Applications Programming developed for Microsoft Office 97 uses Microsoft Visual Basic 4.0 as its foundation. Many elements found in Visual Basic 4.0 are now incorporated into Visual Basic for Applications, such as user defined class modules, user defined collections, and custom properties.

The three member types of all Visual Basic for Applications object models are Methods, Properties, and Events. Events have been expanded beyond the Click, Show, and Change events that were available in earlier versions of Microsoft Excel. Examples of new events are Activate, BeforeDoubleClick, SheetChange, Open, WindowActivate, MouseUp, MouseDown, and Calculate.

</li> Code Behind Objects

Visual Basic for Applications also supports extended objects in Microsoft Excel 97. An extended object is an object that has code attached to it. Workbooks and worksheets can have code attached, as can controls and forms.

Code behind objects uses extended objects to enhance Visual Basic for Applications programming by:

<ul> Directly and exclusively attaching a code module to an object.</li> Directly attaching forms to an object.</li> <li>Using Properties and Methods of the object without need for the name of the object to which the code is attached.</li> <li>Providing a unified user model for writing event handling code for the object and for the controls inside the object.</li></ul>

There are several advantages to code behind objects. Code is moved, copied, and saved with the object to which it is attached. Simpler syntax can be used to refer to properties and methods of the objects because the code is attached to the object. For example, in earlier versions of Microsoft Excel, if programming code referred to "Worksheets("Sheet1")" and Sheet1 was renamed to "First Sheet", all code referring to Sheet1 would generate an error. With code attached to the object, when Sheet1 is renamed, the code attached to it still runs without you having to edit the code.

</li></ul>

<div class="references_section">