Calling functions
You can call server functions for an OLE control through its
Object property using the following syntax:
1 |
<span>olecontrolname</span>.<span>Object</span>.{<span> serverqualifiers</span>.}<span>functionname</span> ( { <span>arguments</span> } ) |
If the OLE object is complex, there could be nested properties
or objects within the object that serve as qualifiers for the function
name.
PowerScript considers all commands to the server either property
settings or functions. For statements and functions to be distinguished
from property settings, they must be followed by parentheses surrounding
the parameters. If there are no parameters, specify empty parentheses.
Arguments and return values and their datatypes
PowerBuilder converts OLE data to and from compatible PowerBuilder datatypes.
The datatypes of values you specify for arguments must be compatible
with the datatypes expected by the server, but they do not need
to be an exact match.
When the function returns a value, you can assign the value
to a PowerBuilder variable of a compatible datatype.
Passing arguments by reference
If an OLE server expects an argument to be passed by reference
so that it can pass a value back to your script, include the keyword REF just
before the argument. This is similar to the use of REF in
an external function declaration:
1 |
<span>olecontrol</span>.Object.<span>functionname</span> ( REF <span>argname</span> ) |
In these generic examples, the server can change the values
of ls_string and li_return because
they are passed by reference:
1 |
string ls_string<br>integer li_return<br>ole_1.Object.testfunc(REF ls_string, REF li_return) |
This example illustrates the same function call using an OLEObject
variable.
1 |
OLEObject ole_obj<br>ole_obj = CREATE OLEObject<br>ole_obj.ConnectToNewObject("servername")<br>ole_obj.testfunc(REF ls_string, REF li_return) |
Calls from a PowerBuilder client to a server time out after
five minutes. You can use the SetAutomationTimeout PowerScript
function to change the default timeout period if you expect a specific
OLE request to take longer.
Word and automation
Microsoft Word 6.0 and 7.0 support automation with a command
set similar to the WordBasic macro language. The command set includes
both statements and functions and uses named parameters. Later versions
of Microsoft Word use Visual Basic for Applications (VBA), which
consists of a hierarchy of objects that expose a specific set of
methods and properties.
WordBasic statements
WordBasic has both statements and functions. Some of them
have the same name. WordBasic syntax differentiates between statements
and functions calls, but PowerBuilder does not.
To specify that you want to call a statement, you can include AsStatement! (a value
of the OLEFunctionCallType enumerated datatype)
as an argument. Using AsStatement! is the only
way to call WordBasic statements that have the same name as a function.
Even when the statement name does not conflict with a function name,
specifying AsStatement! is more efficient:
1 |
<span>olecontrol</span>.<span>Object</span>.application.wordbasic.<span>statementname<br></span>    ( <span>argumentlist</span>, AsStatement! ) |
For example, the following code calls the AppMinimize statement:
1 |
ole_1.Object.application.wordbasic. &<br>   AppMinimize("",1,AsStatement!) |
Named parameters
PowerBuilder does not support named parameters that both WordBasic
and Visual Basic use. In the parentheses, specify the parameter values
without the parameter names.
For example, the following statements insert text at a bookmark
in a Word 6.0 or 7.0 document:
1 |
ole_1.Activate(InPlace!)<br>Clipboard(mle_nameandaddress.Text)<br>ole_1.Object.application.wordbasic.&<br>   fileopen("c:msofficewinworddoc1.doc")<br>ole_1.Object.application.wordbasic.&<br>   editgoto("NameandAddress", AsStatement!)<br>ole_1.Object.application.wordbasic.&<br>   editpaste(1, AsStatement!) |
The last two commands in a WordBasic macro would look like
this, where Destination is the named parameter:
1 |
EditGoto.Destination = "NameandAddress"<br>EditPaste |
In a PowerBuilder script, you would use this syntax to insert
text in a Word 97 or later document:
1 |
ole_1.Object.Selection.TypeText("insert this text") |
In the corresponding Visual Basic statement, the named parameter
Text contains the string to be inserted:
1 |
Selection.TypeText Text:="insert this text" |
You cannot send commands to the server application that declare
variables or control the flow of execution (for example, IF
THEN). Automation executes one command at a time independently
of any other commands. Use PowerScript’s conditional and
looping statements to control program flow.
Example of Word automation
To illustrate how to combine PowerScript with server commands,
the following script counts the number of bookmarks in a Microsoft
Word OLE object and displays their names:
1 |
integer i, count<br>string bookmarklist, curr_bookmark<br>ole_1.Activate(InPlace!)<br> <br>count = ole_1.Object.Bookmarks.Count<br>bookmarklist = "Bookmarks = " + String(count) + "~n"<br>   <br>FOR i = 1 to count<br>   curr_bookmark = ole_1.Object.Bookmarks[i].Name<br>   bookmarklist = bookmarklist + curr_bookmark + "~n"<br>END FOR<br> <br>MessageBox("BookMarks", bookmarklist) |
You can check that you are using the correct syntax for Word
automation with the Word macro editor. Turn on macro recording in
Word, perform the steps you want to automate manually, then turn
off macro recording. You can then type Alt+F11 to open
the macro editor and see the syntax that was built. Remember that
PowerBuilder uses square brackets for array indexes.
Example of Word 6.0 and 7.0 automation
The following script counts the number of bookmarks in a Microsoft
Word 6.0 or 7.0 OLE object and displays their names:
1 |
integer i, count<br>string bookmarklist, curr_bookmark<br>ole_1.Activate(InPlace!)<br> <br>// Get the number of bookmarks<br>count = ole_1.Object. &<br>    application.wordbasic<span>.</span>countbookmarks<br>bookmarklist = "Bookmarks = " + String(count) + "~n"<br> <br>// Get the name of each bookmark<br>FOR i = 1 to count<br>   curr_bookmark = ole_1.Object. &<br>   application.wordbasic<span>.</span>bookmarkname(i)<br>   bookmarklist = bookmarklist    + curr_bookmark + "~n"<br>END FOR<br> <br>MessageBox("BookMarks", bookmarklist) |