Mastering PowerBuilder

HomePrevious Lesson: Mail Merge Using OLE 2.0
Next Lesson: In-Process Versus Out-Process

The PowerBuilder Window

This example is the w_mail_merge_using_ole window in the OLE2-2.PBL library. If you open up the window, you'll see that it contains a DataWindow control containing the address information and just two CommandButtons.

The code for the clicked event of the Mail Merge button is as follows:
Any lResult
Long lTotRows,i,j
String lAddress
// Declare and create ole object.
OLEObject OLEMailMerge
OLEMailMerge = CREATE OLEObject
lResult = OLEMailMerge.ConnectToNewObject( "word.basic" )
If Integer(lResult) <> 0 Then
		DESTROY OLEMailMerge
		MessageBox( "Error in connecting to Word for Windows", &
"Error Code: " + String( lResult ) )
		Return
End If
lTotRows = dw_1.Rowcount()
For i = 1 to lTotRows Step 1
		lAddress = dw_1.GetItemString(i,1)
		For j = 2 to 7 Step 1
				lAddress = lAddress + "~r~n" + &
				dw_1.GetItemString(i,j) 
		Next
		ClipBoard( lAddress )
		lResult = OLEMailMerge.FileOpen( &
				C:\workdir\ole_letter.doc")
		If Integer(lResult) <> 0 Then
				DESTROY OLEMailMerge
				MessageBox("Error in connecting to Word " + &
				"for Windows", "Error Code: " + String( lResult ) )
				Return
				End If
// These are the important commands:
		OLEMailMerge.editgoto( "Address" )
		OLEMailMerge.insert( lAddress )
		OLEMailMerge.FilePrint()
		OLEMailMerge.FileClose( 2 )
Next
lResult = OLEMailMerge.DisConnectObject()
DESTROY OLEMailMerge

When you run this example, it might take long time depending on the your computer and whether MS-Word is already open or not. In this example, we are introducting a new data type "ANY". This variable is used especially, when you don't know the return value of the OLE Server method. Later when you compare with any value, we need to explicitly convert the value as shown in the example. We define OLEMailMerge as an OLE object, connect to the Word document and then send the various server commands. The important lines are:

Code

Description

OLEMailMerge.editgo( "Address" ):

This sends the command to go to the bookmark.

OLEMailMerge.insert( lAddress ):

This inserts the address.

OLEMailMerge.FilePrint():

This prints the document.

OLEMailMerge.FileClose( 2 ):

This closes the document without saving it.

We are closing the document without saving it, so that when we loop around for the next address, we can reopen the document as a clean copy.

Any attribute, function or parameter used against this object are referred to the server. So PowerBuilder doesn't need to know whether they are valid or not and will not check them. If the functions or attributes are invalid, you will get an error at run-time.

When you run this window and click on the Mail Merge button, you won't see much happening, but assuming your computer is connected to a printer, you should get two printed copies of our form letter, one for each of the addresses in our DataWindow.
HomePrevious Lesson: Mail Merge Using OLE 2.0
Next Lesson: In-Process Versus Out-Process