Object Model Syntax

It is time that we formally discuss the basic syntax that is used when programming with an object model.

The general syntax for referring to an object's properties and methods is very simple. If objVar is an object variable that refers to a particular object and AProperty is a property of this object, then we can access this property (for reading or for changing) using the syntax:

objVar.AProperty(any required parameters)

For instance, the following code sets the font name property of the first row in the active worksheet:

' Declare object variable Dim rng As Range

' Set rng to refer to first row Set rng = ActiveSheet.Rows(1)

' Set font name rng.Font.Name = "Arial"

Note that the last line of code actually invokes two properties; the Font property of rng returns a Font object, whose Name property is set to Arial.

If AMethod is a method for this object, then we can invoke that method with the syntax:

objVar.AMethod(any required parameters)

Note that this syntax is quite similar to the syntax used to call an ordinary VBA subroutine or function, except that here we require qualification with the name of the variable that points to the object whose property or method is being called.

For instance, continuing the previous code, we can apply the CheckSpelling method to the row referred to by rng as follows:


We could include the name of a custom dictionary as a parameter to this method.

0 0

Post a comment