OpenOffice.org Macros Explained

The CreateUnoService function creates services in the scope of the OOo application. The interface com.sun.star.lang.XMultiServiceFactory defines the object method createlnstance(), which allows an object to create a service in the scope of the object. The oSettings object in Listing 33 supports the service com.sun.star.text.DocumentSettings. The oSettings object is related to ThisComponent in that the document settings that it reflects are for ThisComponent-the current document-but not for any other document.

Listing 33: Create an object that supports the DocumentSettings service.

oSettings=ThisComponent.createInstance("com.sun.star.text.DocumentSettings")

 

The createlnstance() method returns an object that supports the requested service if it can; if it cannot, it returns NULL. The returned object may support other services as well (see Listing 34 and Figure 14 ).

Figure 14: Some Writer document settings.

Listing 34: WriteDocumentSettings is found in the Generic module in this chapter's source code files as SC12.sxw.

Sub WriteDocumentSettings Dim oSettings 'Settings object to create Dim s$ 'Utility string Dim i% 'Utility index variable Dim v 'This will contain an array of service names REM Create an object that supports the DocumentSettings service oSettings=ThisComponent.createInstance("com.sun.star.text.DocumentSettings") v = oSettings.getSupportedServiceNames() s = "**** specified Supported Services ****" & CHR$(10) & Join(v, CHR$(10)) s = s & CHR$(10) & CHR$(10) & "**** Tested Services ****" & CHR$(10) REM Now check to see if this created object supports any other services. v = Array("com.sun.star.comp.Writer.DocumentSettings",_ "com.sun.star.text.PrintSettings") For i = 0 To UBound(v) If oSettings.supportsService(v(i)) Then s = s & "Supports service " & v(i) & CHR$(10) End If Next MsgBox s, 0, "Some services for " & oSettings.getImplementationName() REM What is the status of the PrintControls property? Print oSettings.PrintControls REM I could set this to True or False 'oSettings.PrintControls = True End Sub

 

A careful inspection of Listing 34 and Figure 14 reveal some unexpected behavior.

Although not shown in Listing 34, the two "Tested Services" in Figure 14 cannot be created using the createlnstance() object method. I discovered the PrintSettings service entirely by accident . I inspected the dbg_properties property, which contained PrintControls. I then searched Google for "site:api.openoffice.org PrintSettings". The important things to notice are as follows :

Категории