OLEObjects for efficiency
When your automation command refers to a deeply nested object
with multiple server qualifiers, it takes time to negotiate the
object’s hierarchy and resolve the object reference. If
you refer to the same part of the object hierarchy repeatedly, then
for efficiency you can assign that part of the object reference to
an OLEObject variable. The reference is resolved once and reused.
Instead of coding repeatedly for different properties:
1 |
ole_1.Object.application.wordbasic.<span>propertyname</span> |
you can define an OLEObject variable to handle all the qualifiers:
1 |
OLEObject ole_wordbasic<br>ole_wordbasic = ole_1.Object.application.wordbasic<br>ole_wordbasic.<span>propertyname1</span> = <span>value</span><br>ole_wordbasic.<span>propertyname2</span> = <span>value</span> |
Example: resolving an object reference
This example uses an OLEObject variable to refer to a Microsoft
Word object. Because it is referred to repeatedly in a FOR loop,
the resolved OLEObject makes the code more efficient. The example
destroys the OLEObject variable when it is done with it:
1 |
integer li_i, li_count<br>string ls_curr_bookmark<br>OLEObject ole_wb<br> <br>ole_1.Activate(InPlace!)<br>ole_wb = ole_1.Object.application.wordbasic<br> <br>// Get the number of bookmarks<br>li_count = ole_wb.countbookmarks<br>// Get the name of each bookmark<br>FOR li_i = 1 to count<br>   ls_curr_bookmark = ole_wb.bookmarkname(i)<br>   ... // code to save the bookmark name in a list<br>END FOR |