Using Properties and Methods

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

To work with the content and functionality exposed by an object, you use properties and methods of that object. The following Excel example uses the Value property of the Range object to set the contents of cell B3 on the Sales worksheet in the Current.xls workbook to 3:

Workbooks("Current.xls").Worksheets("Sales").Range("B3").Value = 3

The following example uses the Bold property of the Font object to apply bold formatting to cell B3 on the Sales worksheet:

Workbooks("Current.xls").Worksheets("Sales").Range("B3").Font.Bold = True

The following Word example uses the Close method of the Document object to close the file named Draft3.doc:

Documents("Draft3.doc").Close

In general, you use properties to set or read the content, which can include the text or value contained in an object, or other attributes of the object, and you use methods to work with an application's (or VBA's) built-in functionality to perform operations on the content. Be aware, however, that this distinction doesn't always hold true; there are a number of properties and methods in every object model that are exceptions to this rule.

Creating Your Own Objects and Object Models

You can create your own objects and object models by creating and using class modules. For example, you may need to work with complex sets of data that need to be managed in a consistent and reliable way. By creating your own objects, properties, and methods to work with this data in a class module, you can create an object model to make working with your data simpler and less error-prone. Similarly, you can create class modules to create wrapper functions around Windows application programming interface (API) calls or even complex parts of existing object models to make them easier to use.

For detailed information about how to work with class modules, see Chapter 9, "Custom Classes and Objects."