Example: The Window Object

Example: The Window Object

Another object that's common to almost all applications is the Window object, which represents an open window in an application. Note that this isn't the same as an open document. Rather, the Window object is just a container for a document, so the associated properties and methods have no effect on the document data. You can use VBA to change the window state (maximized or minimized), size and move windows, navigate open windows, and much more. In the next section, I'll show you how to specify a Window object in your code; you'll also look at some Window object properties and methods.

Specifying a Window Object

If you need to perform some action on a window or change a window's properties, you need to tell the application which window you want to use. VBA gives you two ways to do this:

Use the Windows object— The Windows object is the collection of all the open windows in the application. To specify a window, either use its index number (as given by the numbers beside the windows on the application's Windows menu) or enclose the window caption (in other words, the text that appears in the window's title bar) in quotation marks. For example, if the Budget.doc window is listed first in the Window menu, the following two statements would be equivalent:



Use the ActiveWindow object— The ActiveWindow object represents the window that currently has the focus.

Opening a New Window

If you need to create a new window, use the Window object's NewWindow method:



The Window object from which you want to create the new window.

Note that this argument is optional in some applications. In Word, for example, if you omit Window, the active window is used.

Window Object Properties

Here's a rundown of some common properties associated with Window objects:

Window.Caption— Returns or sets the text that appears in the title bar of the specified Window.

Window.Height— Returns or sets the height, in points, of the specified Window.

Window.Left— Returns or sets the distance, in points, of the left edge of the specified Window from the left edge of the application window.

Window.Top— Returns or sets the distance, in points, of the top of the specified Window from the top of the application window.

Window.UsableHeight— The maximum height, in points, that data can occupy within the specified Window.

Window.UsableWidth— The maximum width, in points, that data can occupy within the specified Window.

Window.Visible— A Boolean value that either hides the specified Window (False) or displays the Window (True).

Window.Width— Returns or sets the width, in points, of the specified Window.

Window.WindowNumber— Returns the window number of the specified Window. For example, a window named Chaptr05.doc:2 has window number 2.

Window.WindowState— Returns or sets the state of the specified Window. See the Application.WindowState property, discussed earlier, for a list of the constants that control this property.

Window Object Methods

Window objects have a few methods that you can use to control your windows programmatically. Here are a few methods that you'll use most often:

Window.Activate— Activates the specified open Window. For example, the following statement activates the Finances.xls window:


Window.Close— Closes the specified Window.

Window.LargeScroll— Scrolls through the specified Window by screens, using the following syntax:

Window.LargeScroll(Down, Up, ToRight, ToLeft)


The Window object you want to scroll through.


The number of screens to scroll down.


The number of screens to scroll up.


The number of screens to scroll to the right.


The number of screens to scroll to the left.

Window.SmallScroll— Scrolls through the specified Window by lines, using the following syntax:

Window.SmallScroll(Down, Up, ToRight, ToLeft)

The arguments are the same as those in the LargeScroll method.

The Absolute Minimum

This chapter discussed the all-important topics of objects and how to work with them in your VBA procedures. After some introductory information on objects and the object hierarchy, you learned about the three types of members of any object class: properties, methods, and events. I also showed you how to assign objects to variables, how to wield the Is operator, and how to work with multiple properties or methods. I closed this chapter with a look at the properties and methods of the Application and Window objects.

One thing I didn't mention but that you may want to check out is the Object Browser. This is a handy tool that shows you the objects available for your procedures, as well as the properties, methods, and events for each object. To display the Object Browser in the Visual Basic Editor, select View, Object Browser (you can also press F2). In the Object Browser dialog box that appears, use the Classes list to select the object you want to see and its properties, methods, and events will appear in the Members list on the right.

Here's a list of chapters where you'll find related information:

  • The With...End With statement is an example of a VBA control structure. I'll discuss a few more of these control structures in Chapter 6, "Controlling Your VBA Code."

  • Part 2, "Putting VBA to Work," is a veritable "object fest" as I examine the object hierarchies in the main Office applications.

     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows