CreateUnoService Function

Instantiates a Uno service with the ProcessServiceManager.

Syntax:


     CreateUnoService(serviceName As String) As Object
  

Return type:

Object

Parameters:

Name

Type

Description

serviceName

String

The service name.

For a list of available services, visit the com::sun::star Module reference page.


Example:

The example below creates the function FileExists that uses the service com.sun.star.ucb.SimpleFileAccess to test if a given path is an existing file.


    Function FileExists(sPath as String) As Boolean
        Dim svcSFA As Object
        Set svcSFA = CreateUnoService("com.sun.star.ucb.SimpleFileAccess")
        Dim bExists As Boolean : bExists = svcSFA.exists(sPath)
        Dim bIsFolder As Boolean : bIsFolder = svcSFA.IsFolder(sPath)
        FileExists = bExists And Not bIsFolder
    End Function ' FileExists
  
tip

UNO services have an extensive online documentation in the api.libreoffice.org website. Visit the SimpleFileAccess Service reference page to learn more about the methods provided by the service used in the example above.


Example:

The following code uses the service com.sun.star.ui.dialogs.FilePicker to show an open file dialog:


Sub Main
   fName = FileOpenDialog ("Please select a file")
   print "file chosen: "+fName
End Sub
 
Function FileOpenDialog(title As String) As String
   res = com.sun.star.ui.dialogs.ExecutableDialogResults
   filepicker = CreateUnoService("com.sun.star.ui.dialogs.FilePicker")
   With filepicker
      .Title = title
      .MultiSelectionMode = True
      .appendFilter("Calc docs","*.ods;*.csv;*.123")
      .appendFilter("Writer or Base docs","*.odt;*.odb")
      .appendFilter("All files","*.*")
       If res.OK = .execute() Then 
          files = .getSelectedFiles()
          FileOpenDialog=files(0)
       EndIf
   End With
End Function ' FileOpenDialog

Please support us!