O'Reilly logo

Writing Word Macros, Second Edition by Steven Roman PhD

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Object Model Syntax

It is time to discuss formally the basic syntax 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 you can access this property (for reading or for changing) using the syntax:

	objVar.AProperty(any required parameters)

For instance, the following code sets the LeftAlign property of the first paragraph in the active document:

	' Declare object variable
	Dim para As Paragraph

	' Set para to refer to first paragraph
	Set para = ActiveDocument.Paragraphs(1)

	' Set left align property to 36 points
	para.LeftAlign = 36
A small portion of the Word object model

Figure 9-1. A small portion of the Word object model

If AMethod is a method for this object, then that method can be invoked 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 it requires 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 OpenUp method to the paragraph referred to by para as follows:

	para.OpenUp

(This method sets the spacing before the paragraph to 12 points.)

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required