Hints for simplifying notations

<< Click to Display Table of Contents >>

Hints for simplifying notations

If you are beginning to wonder whether so much typing is really necessary to address a single document, we can reassure you that it's not! There are several ways to reduce the amount of typing required.

Using the With statement

The first shortcut is to use the With statement when addressing multiple members of the same object.

First, the conventional style:

tm.Application.Left = 100

tm.Application.Top = 50

tm.Application.Width = 500

tm.Application.Height = 300

tm.Application.Options.CreateBackup = True

MsgBox tm.Application.ActiveDocument.Name

This code looks much clearer through use of the With statement:

With tm.Application

 .Left = 100

 .Top = 50

 .Width = 500

 .Height = 300

 .Options.CreateBackup = True

 MsgBox .ActiveDocument.Name

End With

Setting up object variables

The next abbreviation is to create helper object variables for quickly accessing their members. Compare the following statements:

Complicated:

Sub Complicated

 Dim tm As Object

 Set tm = CreateObject("TextMaker.Application")

 tm.Application.Visible = True ' Make TextMaker visible

 tm.Application.Documents.Add ' Add document

 tm.Application.ActiveDocument.Left = 100

 tm.Application.ActiveDocument.Top = 50

 tm.Application.ActiveDocument.Width = 222

 tm.Application.ActiveDocument.Height = 80

End Sub

Easier:

Sub Better

 Dim tm As Object

 Dim NewDocument As Object

 Set tm = CreateObject("TextMaker.Application")

 tm.Application.Visible = True ' Make TextMaker visible

 Set NewDocument = tm.Application.Documents.Add ' Add document

 NewDocument.Left = 100

 NewDocument.Top = 50

 NewDocument.Width = 222

 NewDocument.Height = 80

End Sub

After you created the object variable "NewDocument" in the second example and stored a reference to the new document in it (which conveniently is returned by the Add method of the Documents collection), you can access the new document much more easily through this helper object variable.

Save time by omitting default properties

There is yet another way to reduce the amount of typing required: Each object (for example, Application or Application.Documents) has one of its properties marked as its default property. Conveniently enough, you can always leave out default properties.

The default property of Application, for example, is Name. Therefore, the two following instructions are equivalent:

MsgBox tm.Application.Name     ' displays the name of TextMaker

MsgBox tm.Application          ' does exactly the same

Typically, the property that is used most often in an object has been designated its default property. For example, the most used property of a collection surely is the Item property, as the most common use of collections is to return one of their members. The following statements therefore are equivalent:

MsgBox tm.Application.Documents.Item(1).Name

Finally things are getting easier again!

But it gets even better: Name is the default property of a single Document object (note: "Document", not "Documents"!). Each Item of the Document collection is of the Document type. As Name is the default property of Document, it can be omitted:

MsgBox tm.Application.Documents(1)

Not easy enough yet? OK... Application is the default property of TextMaker. So, let's just leave out Application as well! The result:

MsgBox tm.Documents(1)

This basic knowledge should have prepared you to understand TextMaker's object model. You can now continue with the section TextMaker's object model that contains a detailed list of all objects that TextMaker provides.