Working with Object Properties
Every object has a defining set of characteristics. These characteristics are called the object's
properties
, and they control the appearance and position of the object. For example, each Window object has a
WindowState
property you can use to display a window as maximized, minimized, or normal. Similarly, a Word Document object has a
Name
property to hold the filename, a
Saved
property that
tells
you whether or not the document has changed since the last save, a
Type
property to hold the document type (regular or template), and many more.
|
You'll come across the word "active" quite often in your VBA
travels
, so let's make sure you know what it means. In the VBA world,
active
describes the item you're currently working with. In Word, for example, the document that you're currently working with is the active document. Similarly, in Excel the worksheet cell that you're editing or formatting is the active
cell
. In programming lingo, the active item is said to have the
focus
.
|
When you refer to a property, you use the following syntax:
Object.Property
For example, the following expression refers to the
ActiveWindow
property of the
Application
object:
Application.ActiveWindow
One of the most confusing aspects of objects and properties is that some properties do double-duty as objects. Figure 5.1 uses an Excel example to
illustrate
this. The
Application
object has an
ActiveWindow
property that tells you the name of the active window. However,
ActiveWindow
is also a Window object. Similarly, the
Window
object has an
ActiveCell
property that specifies the active cell, but
ActiveCell
is also a
Range
object. Finally, a
Range
object has a
Font
property, but a font is also an object with its own properties (
Italic
,
Name
,
Size
, and so on).
Figure 5.1. Some Excel properties also can be objects.
In other words, lower-level objects in the object hierarchy are really just properties of their parent objects. This idea will often help you to reduce the length of a hierarchical path (and thus reduce the abuse your typing fingers must bear). For example, consider the following object
path
:
Application.ActiveWindow.ActiveCell.Font.Italic
Here, an object such as
ActiveCell
implicitly refers to the
ActiveWindow
and
Application
objects, so you can knock the path down to size, as
follows
:
ActiveCell.Font.Italic
Setting the Value of a Property
To set a property to a certain value, you use the following syntax:
Object.Property=value
Here,
value
is an expression that returns the value to which you want to set the property. As such, it can be any of VBA's recognized data types, including the following:
-
A numeric value. For example, the following statement sets the size of the font in the active cell to 14:
ActiveCell.Font.Size = 14
-
A string value. The following example sets the font name in the active cell to Times New Roman:
ActiveCell.Font.Name = "Times New Roman"
-
A logical value (in other words, True or False). The following statement turns on the
Italic
property in the active cell:
ActiveCell.Font.Italic = True
Returning the Value of a Property
Sometimes you need to know the current setting of a property before changing the property or performing some other action. You can find out the current value of a property by using the following syntax:
variable=Object.Property
Here,
variable
is a variable or another property. For example, the following statement stores the contents of the active cell in a variable named
cellContents
:
cellContents = ActiveCell.Value
|