Qualifying server commands

Whether to qualify the server command with the name of the application depends on the server and how the object is connected. Each server implements its own version of an object hierarchy, which needs to be reflected in the command syntax. For example, the Microsoft Excel object hierarchy is shown in Figure 19-2.

Figure 19-2: Microsoft Excel object hierarchy

The Excel object hierarchy has the application at the top, with sheet1, sheet2, and sheet3 beneath it. Through an OLE connection, Excel is connected to the OLE 2.0 control within the PowerBuidler window in My Application.

When the server is Excel, the following commands appear to mean the same thing but can have different effects (for an Excel 95 spreadsheet, the cells’ row and column arguments are in parentheses instead of square brackets):

ole_1.Object.application.cells[1,2].value = 55
ole_1.Object.cells[1,2].value = 55

The first statement changes a cell in the active document. It moves up Excel’s object hierarchy to the Application object and back down to an open sheet. It does not matter whether it is the same one in the PowerBuilder control. If the user switches to Excel and activates a different sheet, the script changes that one instead. You should avoid this syntax.

The second statement affects only the document in the PowerBuilder control. However, it will cause a runtime error if the document has not been activated. It is the safer syntax to use, because there is no danger of affecting the wrong data.

Microsoft Word 6.0 and 7.0 implement the application hierarchy differently and require the qualifier application.wordbasic when you are manipulating an object in a control. (You must activate the object.) For example:

ole_1.Object.application.wordbasic.bookmarkname(i)

Later versions of Microsoft Word do not require a qualifier, but it is valid to specify one. You can use any of the following syntaxes:

ole_1.Object.Bookmarks.[i].Name
ole_1.Object.Bookmarks.item(i).Name

ole_1.Object.application.ActiveDocument. &    
   Bookmarks.[i].Name

When you are working with PowerBuilder’s OLEObject, rather than an object in a control, you omit the application qualifiers in the commands because you have already specified them when you connected to the object. (For more about the OLEObject object type, see “Programmable OLE Objects”.)