Mastering PowerBuilder

HomePrevious Lesson: OLE 2.0 Automation
Next Lesson: Mail Merge Using OLE 2.0

OLE Objects

OLE 2.0 control in the window is used when there is a need for the user to interact with the OLE Server application. When there is no user interaction required with the OLE Server, i.e., whenever you need automation, we can modify OLE Server's objects in the memory through the script. The following simple example retrieves data from the DataWindow and saves the data in word document with separate passwords to open the document and write to the document using the OLE Object.
OLEObject lOLEObject
lOLEObject = Create OLEObject
SetPointer( HourGlass! )
dw_1.SetTransObject( SQLCA )
dw_1.Retrieve()
dw_1.SaveAs( "c:\workdir\results.txt", Text!, True )
lOLEObject.ConnectToNewObject( "word.basic" )
lOLEObject.FileOpen( "c:\workdir\test2.doc" )
lOLEObject.Insert( "Results" + "~n")
lOLEObject.InsertFile( "c:\workdir\results.txt" )
lOLEObject.FileSaveAs( "c:\workdir\results.doc", 0, 0,&
			"password", 0, "write_password" )
			lOLEObject.FileClose()
Destroy lOLEObject
SetPointer( Arrow! )

Above example saves the DataWindow results to "RESULTS.TXT". The function ConnectToNewObject() creates an empty object of the specified class and connects to that.

After connecting to the specified class, all the commands that are given at the OLE Object level belongs to the connected class. For example, FileOpen() is the Word command. PowerBuilder doesn't check for the existence of the command or syntax errors. However, if the command is not existing at run-time or there is any error in the syntax, it will result in the run-time error. The command "FileSaveAs" saves the file to the specified file. PowerBuilder doesn't support OLE Server's statements, it supports only the functions. If there is any statement, you still need to use parenthesis. The actual command to save the file in the Word is as follows:
FileSaveAs [.Name = text][,.Format = number]
[,.LockAnnot = number][,.Password = text]
[,.WritePassword = text][,.AddToMru = number]
[,.RecommendReadOnly = number]

The equaling command to save the file with passwords in Word 6.0 is:
FileSaveAs .Name = "c:\workdir\results.doc", .Password = "Password",
.WritePassword = "write_password"

Wherever we want to leave to the default values, we need to supply 0 (Zero), otherwise, specify the actual value. The above PowerBuilder code example saves the file and also specifies a password to open the file and another password to allow the user to write to the file. After running this script, if you open the file, it will prompt for two passwords, one to open the file and another is write password.

The following example finds words "WordToReplace" and replaces with the word "NewWord". The last argument 1 says, replace every occurrence of the specified word.
lOLEObject.EditReplace ( "WordToReplace", "NewWord", 0,0,0,0,0,0,0,1 )

However, you should not run commands that allocate memory in the OLE Server. For example, declaring a word variable in the Word Server (OLE Server). If you need something like that, declare those variables in the PowerScript and change the logic accordingly. To learn more about WordBasic for automation, refer to Word documentation and/or Word 6.0 developers kit.

If you want to connect to the existing object, you can use "ConnectToObject" function.

The following example illustrates OLE Automation.
HomePrevious Lesson: OLE 2.0 Automation
Next Lesson: Mail Merge Using OLE 2.0