Programmable OLE Objects
You don’t need to place an OLE control on a window
to manipulate an OLE object in a script. If the object doesn’t
need to be visible in your PowerBuilder application, you can create
an OLE object independent of a control, connect to the server application,
and call functions and set properties for that object. The server
application executes the functions and changes the object’s
properties, which changes the OLE object.
For some applications, you can specify whether the application
is visible. If it is visible, the user can activate the application
and manipulate the object using the commands and tools of the server
application.
OLEObject object type
PowerBuilder’s OLEObject object type is designed for
automation. OLEObject is a dynamic object type, which means that
the compiler will accept any property names, function names, and
parameter lists for the object. PowerBuilder doesn’t have
to know whether the properties and functions are valid. This allows
you to call methods and set properties for the object that are known
to the server application that created the object. If the functions
or properties don’t exist during execution, you will get
execution errors.
Using an OLEObject variable involves these steps:
- Declare the variable and instantiate it.
- Connect to the OLE object.
- Manipulate the object as appropriate using the OLE
server’s properties and functions. - Disconnect from the OLE object and destroy the variable.
These steps are described next.
Declaring an OLEObject variable
You need to declare an OLEObject variable and allocate memory
for it:
1 |
OLEObject myoleobject |
1 |
myoleobject = CREATE OLEObject |
The Object property of the OLE container controls (OLEControl
or OLECustomControl) has a data type of OLEObject.
Connecting to the server
You establish a connection between the OLEObject object and
an OLE server with one of the ConnectToObject functions. Connecting
to an object starts the appropriate server:
When you want to | Choose this function |
---|---|
Create a new object for an OLE server that you specify. Its purpose is similar to InsertClass for a control |
ConnectToNewObject |
Creates a new OLE object in the specified remote server application if security on the server allows it and associates the new object with a PowerBuilder OLEObject variable |
ConnectToNewRemoteObject |
Open an existing OLE object from a file. If you don’t specify an OLE class, PowerBuilder uses the file’s extension to determine what server to start |
ConnectToObject |
Associates an OLE object with a PowerBuilder OLEObject variable and starts the remote server application |
ConnectToRemoteObject |
Remote objects The ConnectToRemoteObject and ConnectToNewRemoteObject functions
are available on Windows NT 4.x
or greater,
Windows 98, and Windows 95 with the DCOM update.
After you establish a connection, you can use the server’s
command set for automation to manipulate the object (see “OLE objects in scripts “).
You don’t need to include application qualifiers
for the commands. You already specified those qualifiers as the
application’s class when you connected to the server. For
example, the following commands create an OLEObject variable, connect
to Microsoft Word 97’s OLE interface (word.application),
open a document and display information about it, insert some text,
save the edited document, and shut down the server:
1 |
OLEObject o1 |
1 |
string s1 |
1 |
o1 = CREATE oleobject |
1 |
1 |
o1.ConnectToNewObject("word.application") |
1 |
o1.documents.open("c: emp emp.doc") |
1 |
1 |
// Make the object visible and display the |
1 |
// MS Word user name and filename |
1 |
o1.Application.Visible = True |
1 |
s1 = o1.UserName |
1 |
MessageBox("MS Word User Name", s1) |
1 |
s1 = o1.ActiveDocument.Name |
1 |
MessageBox("MS Word Document Name", s1) |
1 |
1 |
//Insert some text in a new paragraph |
1 |
o1.Selection.TypeParagraph() |
1 |
o1.Selection.typetext("Insert this text") |
1 |
o1.Selection.TypeParagraph() |
1 |
1 |
// Insert text at the first bookmark |
1 |
o1.ActiveDocument.Bookmarks[1].Select |
1 |
o1.Selection.typetext("Hail!") |
1 |
1 |
// Insert text at the bookmark named End |
1 |
o1.ActiveDocument.Bookmarks.item("End").Select |
1 |
o1.Selection.typetext("Farewell!") |
1 |
1 |
// Save the document and shut down the server |
1 |
o1.ActiveDocument.Save() |
1 |
o1.quit() |
1 |
RETURN |
For earlier versions of Microsoft Word, use word.basic instead
of word.application. The following commands connect to Microsoft
Word 7.0’s OLE interface (word.basic), open a document,
go to a bookmark location, and insert the specified text:
1 |
myoleobject.ConnectToNewObject("word.basic") |
1 |
myoleobject.fileopen("c: empletter1.doc") |
1 |
myoleobject.editgoto("NameAddress") |
1 |
myoleobject.Insert("Text to insert") |
Do not
include word.application or word.basic
(the class in ConnectToNewObject) as a qualifier:
1 |
// Incorrect command qualifier |
1 |
myoleobject.word.basic.editgoto("NameAddress") |
Microsoft Word 7.0 implementation For an OLEObject variable, word.basic is the class name of
Word 7.0 as a server application. For an object in a control, you
must use the qualifier application.wordbasic to tell Word how to
traverse its object hierarchy and access its wordbasic object.
Shutting down and disconnecting from the server
After your application has finished with the automation, you
may need to explicitly tell the server to shut down. This is the
case with Microsoft Word 97. You can also disconnect from the server
and release the memory for the object:
1 |
myoleobject.Quit() // required by Word 97 |
1 |
rtncode = myoleobject.DisconnectObject() |
1 |
DESTROY myoleobject |
You can rely on garbage collection to destroy the OLEObject
variable. Destroying the variable automatically disconnects from
the server.
It’s preferable to use garbage collection to destroy
objects, but if you want to release the memory used by the variable
immediately and you know that it is not being used by another part
of the application, you can explicitly disconnect and destroy the
OLEObject variable, as shown in the code above.
For more information, see “Garbage collection”.
Assignments among OLEControl, OLECustomControl,
and OLEObject data types
You cannot assign an OLE control (object type OLEControl)
or ActiveX control (object type OLECustomControl) to an OLEObject.
If the vendor of the control exposes a programmatic identifier
(in the form vendor.application), you could specify this identifier
in the ConnectToNewObject function to connect to the programmable
interface without the visual control. For an ActiveX control with
events, this technique makes the events unavailable. ActiveX controls
are not meant to be used this way and would not be useful in most
cases.
You can assign the Object property of an OLE control to an
OLEObject variable or use it as an OLEObject in a function. For
example, if you have an OLEControl ole_1 and an OLECustomControl
ole_2 in a window and you have declared this variable:
1 |
OLEObject oleobj_automate |
you can make these assignments:
1 |
oleobj_automate = ole_1.Object |
1 |
oleobj_automate = ole_2.Object |
You cannot assign an OLEObject to the Object property of an
OLE control because it is read-only. You cannot make this assignment:
1 |
ole_1.Object = oleobj_automate //Error! |
Events for OLEObjects
You can implement events for an OLEObject by creating a user
object that is a descendant of OLEObject. The SetAutomationPointer
PowerScript function assigns an OLE automation pointer into the
descendant so that it can use OLE automation.
Suppose oleobjectchild is a descendant of OLEObject that implements
events such as the ExternalException and Error events. The following
code creates an OLEObject and an instance of oleobjectchild, which
is a user object that is a descendant of OLEObject, connects to
Excel, then assigns the automation pointer to the oleobjectchild:
1 |
OLEObject ole1 |
1 |
oleobjectchild oleChild |
1 |
1 |
ole1 = CREATE OLEObject |
1 |
ole1.ConnectToNewObject( "Excel.Application") |
1 |
1 |
oleChild = CREATE oleobjectchild |
1 |
oleChild.SetAutomationPointer( ole1 ) |
You can now use olechild for automation.
Automation scenario
The steps involved in automation can be included in a single
script or be the actions of several controls in a window. If you
want the user to participate in the automation, you might:
- Declare an OLE object as an instance variable of a window.
- Instantiate the variable and connect to the server
in the window’s Open event. - Send commands to the server in response to the user’s
choices and specifications in listboxes or edit boxes. - Disconnect and destroy the object in the window’s
Close event.
If the automation doesn’t involve the user, all the
work can be done in a single script, as shown in the following form
letter example.
Example: generating form letters via OLE
This example takes names and addresses from a DataWindow object
and letter body from a MultiLineEdit and creates and prints letters
in Microsoft Word 6.0 or 7.0 using word.basic.
To set up the form letter example:
-
Create a Word document called CONTACT.DOC
with four bookmarks and save the file in your PowerBuilder directory:- name1–for
the name in the return address - name2–for the name in the salutation
- address1–for the street, city, state, and
zip in the return address - body–for the body of the letter
- Multimedia Promotions,
Inc. - 1234 Technology Drive
- Westboro, Massachusetts
- January 12, 1995
- [bookmark name1]
- [bookmark address1]
- Dear [bookmark name2]:
- [bookmark body]
- Sincerely,
- Harry Mogul
- President
You could spruce up the letter with a company and a signature
logo. The important items are the names and placement of the bookmarks. - name1–for
-
In PowerBuilder, define a DataWindow object called
d_maillist that has the following columns:- id
- first_name
- last_name
- street
- city
- state
- zip
You can turn on Prompt for Criteria in the DataWindow object
so the user can specify the customers who will receive the letters. -
Define a window that includes a DataWindow control
called dw_mail, a MultiLineEdit called mle_body,
and a CommandButton or PictureButton: -
Assign the DataWindow object d_maillist
to the DataWindow control dw_mail. -
Write a script for the window’s Open
event that connects to the database and retrieves data for the DataWindow
object. The following code connects to an Adaptive Server Anywhere database. (When
the window is part of a larger application, the connection is typically
done by the application Open script.)1/**************************************************1Set up the transaction object from the INI file1**************************************************/1SQLCA.DBMS=ProfileString("PB.INI", &1"Database", "DBMS", " ")1SQLCA.DbParm=ProfileString("PB.INI", &1"Database", "DbParm", " ")1/**************************************************1Connect to the database and test whether the connect succeeded1**************************************************/1CONNECT USING SQLCA;1IF SQLCA.SQLCode <> 0 THEN1MessageBox("Connect Failed", &1"Cannot connect to database. " &1+ SQLCA.SQLErrText)1RETURN1END IF1/**************************************************1Set the transaction object for the DataWindow control and retrieve data1**************************************************/1dw_mail.SetTransObject(SQLCA)1dw_mail.Retrieve() -
Write the script for the Generate Letters button
(the script is shown below).The script does all the work, performing the following tasks:
- Creates the OLEObject variable
- Connects to the server (word.basic)
- For each row in the DataWindow object, it generates
a letter. To do so, it uses WordBasic statements to perform the
following tasks:WordBasic statements Task fileopen Opens the document with the bookmarks editgoto and insert Extracts the name and address information
from a row in the DataWindow object and inserts it into the appropriate
places in the lettereditgoto and insert Inserts the text the user types in mle_body
into the letterfileprint Prints the letter fileclose(2) Closes the letter document without saving
it - Disconnects from the server
- Destroys the OLEObject variable
-
Write a script for the Close button. All it needs
is one command:1Close(Parent)
Script for generating form letters
The following script generates and prints the form letters:
1 |
OLEObject contact_ltr |
1 |
integer result, n |
1 |
string ls_name, ls_addr |
1 |
/*************************************************** |
1 |
Allocate memory for the OLEObject variable |
1 |
***************************************************/ |
1 |
contact_ltr = CREATE oleObject |
1 |
/*************************************************** |
1 |
Connect to the server and check for errors |
1 |
***************************************************/ |
1 |
result = & |
1 |
contact_ltr.ConnectToNewObject("word.basic") |
1 |
IF result <> 0 THEN |
1 |
DESTROY contact_ltr |
1 |
MessageBox("OLE Error", & |
1 |
"Unable to connect to Microsoft Word. " & |
1 |
+ "Code: " & |
1 |
+ String(result)) |
1 |
RETURN |
1 |
END IF |
1 |
/*************************************************** |
1 |
For each row in the DataWindow, send customer |
1 |
data to Word and print a letter |
1 |
***************************************************/ |
1 |
FOR n = 1 to dw_mail.RowCount() |
1 |
/************************************************ |
1 |
Open the document that has been prepared with |
1 |
bookmarks |
1 |
************************************************/ |
1 |
contact_ltr.fileopen("c:pb7contact.doc") |
1 |
/************************************************ |
1 |
Build a string of the first and last name and |
1 |
insert it into Word at the name1 and name2 |
1 |
bookmarks |
1 |
************************************************/ |
1 |
ls_name = dw_mail.GetItemString(n, "first_name")& |
1 |
+ " " + dw_mail.GetItemString(n, "last_name") |
1 |
contact_ltr.editgoto("name1") |
1 |
contact_ltr.insert(ls_name) |
1 |
contact_ltr.editgoto("name2") |
1 |
contact_ltr.insert(ls_name) |
1 |
/************************************************ |
1 |
Build a string of the address and insert it into |
1 |
Word at the address1 bookmark |
1 |
************************************************/ |
1 |
ls_addr = dw_mail.GetItemString(n, "street") & |
1 |
+ "~r~n" & |
1 |
+ dw_mail.GetItemString(n, "city") & |
1 |
+ ", " & |
1 |
+ dw_mail.GetItemString(n, "state") & |
1 |
+ " " & |
1 |
+ dw_mail.GetItemString(n, "zip") |
1 |
contact_ltr.editgoto("address1") |
1 |
contact_ltr.insert(ls_addr) |
1 |
/************************************************ |
1 |
Insert the letter text at the body bookmark |
1 |
***********************************************/ |
1 |
contact_ltr.editgoto("body") |
1 |
contact_ltr.insert(mle_body.Text) |
1 |
/************************************************ |
1 |
Print the letter |
1 |
************************************************/ |
1 |
contact_ltr.fileprint() |
1 |
/************************************************ |
1 |
Close the document without saving |
1 |
************************************************/ |
1 |
contact_ltr.fileclose(2) |
1 |
NEXT |
1 |
/*************************************************** |
1 |
Disconnect from the server and release the memory for the OLEObject variable |
1 |
***************************************************/ |
1 |
contact_ltr.DisconnectObject() |
1 |
DESTROY contact_ltr |
Running the example
To run the example, write a script for the Application object
that opens the window or use the Run/Preview button on
the PowerBar.
When the application opens the window, the user can specify
retrieval criteria to select the customers who will receive letters.
After entering text in the MultiLineEdit for the letter body, the
user can click on the Generate Letters button to print letters for
the listed customers.