|
|
Automating OpenOffice.orgIntroductionThe OpenOffice.org (OOo) supports Microsoft's Automation technology on different Windows platforms ( Windows 95,98, ME, 2000, NT4). It enables clients to control the office externally. Client programs can be contained within executables or scripts. In order to make use of the Automation capability, a client must be coded in a programming language that supports Automation. There are a variety of appropriate languages and development environments available, such as Visual C++, Visual Basic, Delphi, VBScript and JScript. In order to use a scripting language one needs a script controller that executes the script. Common controllers are the Internet Explorer as well as the Windows Script Host (WSH). To give you an impression on how Automation works with OOo, here is a quick example:
'The service manager is always the starting point
This script opens a new writer document and inserts some text. If OOo is not already
running, then an instance is started up automatically. cscript test.vbs in a command line window. Alternatively one can double click the file entry in the Explorer (if in doubt, look at the documentation at http://msdn.microsoft.com/scripting/default.htm). As you may have noticed, this examples is written in VBScript but you can also use JScript with the WSH. Automation ObjectsIn order to do automating tasks by an external client one needs to know what automation objects are offered by the server. Currently the The StarOffice Programmers Tutorial provides this information. Service ManagerAs shown in the example at the beginning of this document, one creates a service manager first. The service manager is the starting point for all external automation tasks. It can be instantiated as every ordinary ActiveX control. How this is done, depends on the programming language being used. The WSH, for example, provides a function on the WScript object that performs instantiation. Set objServiceManager= WScript.CreateObject("com.sun.star.ServiceManager")
This function can also be used in JScript when it is run by the WSH.
Alternatively one can use the var objServiceManager= new ActiveXObject("com.sun.star.ServiceManager"); Once instantiated, the service manager allows access to different office components, for example Set objDesktop= objServiceManager.createInstance("com.sun.star.frame.Desktop") This is essentially the same as calling objDesktop= createunoservice("com.sun.star.frame.Desktop") as is done within StarBasic (see StarOffice Programmers Tutorial). Creation of Types in untyped LanguagesLanguages, such as VBScript and JScript, do not provide types. That is, the declaration of variables do not require a type specifier:
//JScript
In some rare situations you might need to have a concrete type. One can create them in two ways:
Set objServiceManager= WScript.CreateObject("com.sun.star.ServiceManager") The other way goes like this:
Set objServiceManager= WScript.CreateObject("com.sun.star.ServiceManager")
The second approach uses a special object, which was dubbed
You should try to use concrete types when a function that takes an Background InformationThe interfaces of the office usually take parameters of concrete types.
However, there is a type, the
void funcA( float f); When funcA is called in a script, then the OLE bridge receives a An example: someObj.funcB 3.14 The bridge will receive a In most cases the bridge will be able to do a correct conversion. However, it will not work as shown in example above. A possible source of errors is the Creation of StructsIf the OOo API requires a struct as argument then the struct has to be
obtained from the office. It is not possible to declare a struct oneself. To
make this more intelligible, let us assume there is an office function that
takes a struct of type
// the interface function, that will be called from script and the struct is declared as follows:
// idl You cannot write code like this ( VBScript):
Class Size There are to ways to create the struct
Set objServiceManager= WScript.CreateObject("com.sun.star.ServiceManager") And this is the other way
Set objServiceManager= WScript.CreateObject("com.sun.star.ServiceManager")
The Out ParameterLots of interface functions take out or in/out - parameter. In some languages, such as Jscript and Java, those function arguments are not supported. To use those functions regardless, we specified that in/out and out parameters are passed as arrays. The example below shows how this is done in JScript.
// the function takes an out-parameter As one can tell from the examples, the value of the out-parameter is accessible at index 0 within the array. For in/out - parameters, one puts the in - value at index 0 of the array. One can also use Listener Objects (Event Sinks)Some components send events. In order to receive events, one has to implement a listener. A listener is distinguished by implementing a listener interface. To receive those events, one has to connect the event source with the listener. An event source usually offers a function, that takes the listener interface as argument. One only has to implement the listener interface and pass an instance of the listener as argument to the appropriate function. This does not only work with listeners. It is possible to implement all other interfaces an pass them to the office component. Currently, those interfaces can only be implemented in C++ and in JScript. You can find more details in the documentation about the OLE bridge. Visual Basic specific hintsThe UNO interface functions which do not have a return value (i.e.
return
// definition of UNO function Examples |


