Previous Document Next Document

Understanding automation : How is automation coding structured? : Referencing objects


Referencing objects

If you want to create a reference to an object so that you can treat that reference like a variable (sh, in the following VBA example), you can use the Set keyword.

Dim sh As Shape
Set sh = ActiveSelection.Shapes.Item(1)

After you create this reference, you can treat it as though it were the object itself.

sh.Outline.Color.GrayAssign 35

If the selection is changed while sh is still in scope, sh references the original shape from the old selection and is unaffected by the new selection. You cannot simply assign the object to the variable as in the following example:

Dim sh As Shape
sh = ActiveSelection.Shapes.Item(1)

To release an object, you must set its reference value to Nothing.

Set sh = Nothing

You can also test whether a variable references a valid object by using the Nothing keyword.

If sh Is Nothing Then MsgBox "sh is de-referenced."

Objects do not need to be explicitly released. In most cases, VB releases the object upon disposal of the variable when you exit the function or subroutine.

Previous Document Next Document Back to Top

Copyright 2013 Corel Corporation. All rights reserved.