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
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.)
Get Writing Word Macros, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.