Example: generating form letters using 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 using VBA scripting.
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.These are the bookmarks:
-
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
The letter should have the following content:
1Multimedia Promotions, Inc.<br>1234 Technology Drive<br>Westboro, Massachusetts<br>January 12, 2003<br> <br>[bookmark name1]<br>[bookmark address1]<br> <br>Dear [bookmark name2]:<br>[bookmark body]<br> <br>Sincerely,<br><span>Harry Mogul<br></span>PresidentYou could enhance the letter with a company and a signature
logo. The important items are the names and placement of the bookmarks. -
-
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 a SQL Anywhere database.
(When the window is part of a larger application, the connection
is typically done by the application Open script.)1/**************************************************<br>Set up the Transaction object from the INI file<br>**************************************************/<br>SQLCA.DBMS=ProfileString("myapp.ini", &<br>   "Database", "DBMS", " ")<br> <br>SQLCA.DbParm=ProfileString("myapp.ini", &<br>   "Database", "DbParm", " ")<br>/**************************************************<br>Connect to the database and test whether the <br>connect succeeded<br>**************************************************/<br>CONNECT USING SQLCA;<br>IF SQLCA.SQLCode <> 0 THEN<br>   MessageBox("Connect Failed", "Cannot connect" &<br>      + "to database. " + SQLCA.SQLErrText)<br>   RETURN<br>END IF<br>/**************************************************<br>Set the Transaction object for the DataWindow control and retrieve data<br>**************************************************/<br>dw_mail.SetTransObject(SQLCA)<br>dw_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.application)
-
For each row in the DataWindow object, generates
a letterTo do so, it uses VBA statements to perform the tasks in Table 19-5.
Table 19-5: Script tasks VBA statements
Task
open
Opens the document with the bookmarks
goto and typetext
Extracts the name and address information
from a row in the DataWindow object and inserts it into the appropriate
places in the lettergoto and typetext
Inserts the text the user types in mle_body into the
letterprintout
Prints the letter
close
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<br>integer result, n<br>string ls_name, ls_addr<br>/***************************************************<br>Allocate memory for the OLEObject variable<br>***************************************************/<br>contact_ltr = CREATE oleObject<br>/***************************************************<br>Connect to the server and check for errors<br>***************************************************/<br>result = &<br>   contact_ltr.ConnectToNewObject("word.application")<br>IF result <> 0 THEN<br>   DESTROY contact_ltr<br>   MessageBox("OLE Error", &<br>      "Unable to connect to Microsoft Word. " &<br>      + "Code: " &<br>      + String(result))<br>      RETURN<br>END IF<br>/***************************************************<br>For each row in the DataWindow, send customer<br>data to Word and print a letter<br>***************************************************/<br>FOR n = 1 to dw_mail.RowCount()<br>/************************************************<br>   Open the document that has been prepared with<br>   bookmarks<br>************************************************/<br>   contact_ltr.documents.open("c:pbdocscontact.doc")<br>/************************************************<br>   Build a string of the first and last name and<br>   insert it into Word at the name1 and name2<br>   bookmarks<br>************************************************/<br>   ls_name = dw_mail.GetItemString(n, "first_name")&<br>   + " " + dw_mail.GetItemString(n, "last_name")<br>   contact_ltr.Selection.goto("name1")<br>   contact_ltr.Selection.typetext(ls_name)<br>   contact_ltr.Selection.goto("name2")<br>   contact_ltr.Selection.typetext(ls_name)<br>/************************************************<br>   Build a string of the address and insert it into<br>   Word at the address1 bookmark<br>************************************************/<br>   ls_addr = dw_mail.GetItemString(n, "street") &<br>      + "~r~n" &<br>      + dw_mail.GetItemString(n, "city") &<br>      + ", " &<br>      + dw_mail.GetItemString(n, "state") &<br>      + " " &<br>      + dw_mail.GetItemString(n, "zip")<br>   contact_ltr.Selection.goto("address1")<br>   contact_ltr.Selection.typetext(ls_addr)<br>/************************************************<br>   Insert the letter text at the body bookmark<br>***********************************************/<br>   contact_ltr.Selection.goto("body")<br>   contact_ltr.Selection.typetext(mle_body.Text)<br>/************************************************<br>   Print the letter<br>************************************************/<br>   contact_ltr.Application.printout()<br>/************************************************<br>   Close the document without saving<br>************************************************/<br>   contact_ltr.Documents.close<br>   contact_ltr.quit()<br>NEXT<br>/***************************************************<br>Disconnect from the server and release the memory for the OLEObject variable<br>***************************************************/<br>contact_ltr.DisconnectObject()<br>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.