Contingut

# Servei SFDocuments.Base

The Base service provides a number of methods and properties to facilitate the management and handling of LibreOffice Base documents.

This service is closely related to the Document service, which provides generic methods for handling LibreOffice documents, including Base documents. Hence, the Base service extends the Document service and provides additional methods that are specific for Base documents, enabling users to:

- Get access to the database contained in a Base document.

- Open form documents stored in a Base document.

- Check if a form document from a Base document is currently loaded.

Refer to the Document service to learn more about methods and properties that can be used to manage LibreOffice documents.

## Invocació del servei

Abans d'utilitzar el servei Base, cal carregar o importar la biblioteca ScriptForge:

• Per a carregar macros BASIC cal fer servir la biblioteca ScriptForge mitjançant aquesta expressió:
GlobalScope.BasicLibraries.loadLibrary("ScriptForge")
• Els scripts Python requereixen una importació del mòdul scriptforge:
from scriptforge import CreateScriptService

##### En Basic

The Base service can be invoked in a variety of ways. The code snippet below uses the method CreateBaseDocument from the UI service to create a new Base file.

Note that in all examples the object oDoc is an instance of the Base service.

        Dim ui As Object, oDoc As Object
        Set ui = CreateScriptService("UI")
        Set oDoc = ui.CreateBaseDocument("C:\Documents\MyFile.odb")

The Base service can also be instantiated while opening an existing Base file, as shown below:

        Set oDoc = ui.OpenBaseDocument("C:\Documents\MyFile.odb")

If a Base document is already open, it is possible to instantiate the Base service directly:

        Dim oDoc As Object
        Set oDoc = CreateScriptService("SFDocuments.Document", "MyFile.odb")

##### En Python

Els exemples anteriors es poden traduir a Python d'aquesta manera:

        from scriptforge import CreateScriptService
        ui = CreateScriptService("UI")
        doc = ui.CreateBaseDocument(r"C:\Documents\MyFile.odb")

        doc = ui.OpenBaseDocument(r"C:\Documents\MyFile.odb")

        doc = CreateScriptService("SFDocuments.Document", "MyFile.odb")

The use of the "SFDocuments." substring in the previous example is optional.

Llista de mètodes del servei Base
CloseFormDocument FormDocuments Forms GetDatabase | IsLoaded OpenFormDocument OpenQuery OpenTable | PrintOut SetPrinter

## CloseFormDocument

Closes the given form document. Returns True if closure is successful.

The method CloseFormDocument is deprecated since LibreOffice 7.6. Although it is still available, it may be removed from the Base service in a future release. Use the CloseDocument method from the FormDocument service instead.

### Sintaxi:

svc.CloseFormDocument(formdocument: str): bool

### Paràmetres:

formdocument: The name of the FormDocument to be closed, as a case-sensitive string.

### Exemple:

Si els documents de formulari estan organitzats en carpetes, cal incloure el nom de la carpeta per a especificar el document de formulari que s'obrirà, com s'il·lustra en els exemples següents:

##### En Basic

        oDoc.CloseFormDocument("Folder1/myFormDocument")

##### En Python

        doc.CloseFormDocument('Folder1/myFormDocument')

## FormDocuments

Returns an array with the full names (path/name) of all form documents in the Base document as a zero-based Array of strings.

### Sintaxi:

svc.FormDocuments(): str[0..*]

### Exemple:

The code snippet below prints the names of all form documents in the current Base document.

##### En Basic

        Dim oDoc as Object, myForms as Object, formName as String
        Set oDoc = CreateScriptService("Document", ThisDataBaseDocument)
        Set myForms = oDoc.FormDocuments()
        For Each formName In myForms
            MsgBox formName
        Next formName

##### En Python

        bas = CreateScriptService("Basic")
        doc = CreateScriptService("Document", bas.ThisDataBaseDocument)
        myForms = doc.FormDocuments()
        for formName in myForms:
            bas.MsgBox(formName)

To learn more about form documents, refer to ScriptForge.FormDocument service help page.

## Forms

En funció dels paràmetres fornits, aquest mètode retornarà:

- A zero-based Array with the names of all the forms contained in a form document (if the Form argument is absent)

- Un objecte SFDocuments.Form que representa el formulari que s'ha especificat a l'argument Form.

The method Forms is deprecated since LibreOffice 7.6. Although it is still available, it may be removed from the Base service in a future release. Use the Forms method from the FormDocument service instead.

### Sintaxi:

svc.Forms(formdocument: str): str[0..*]

svc.Forms(formdocument: str, form: str = ''): svc

svc.Forms(formdocument: str, form: int): svc

### Paràmetres:

formdocument: The name of a valid form document as a case-sensitive string.

form: The name or index number of the form stored in the form document. If this argument is absent, the method will return a list with the names of all forms available in the form document.

Although it is possible to use index numbers to refer to forms, this is only recommended when there is just one form in the form document. If there are two or more forms, it is preferable to use the form name instead.

### Exemple:

The first line of the example below returns a list of all forms in the form document "myFormDocument". The second line returns an instance of the Form service representing the form "myForm".

##### En Basic

        Dim formsList as Object : formsList = oDoc.Forms("myFormDocument")
        Dim oForm as Object : oForm = oDoc.Forms("myFormDocument", "myForm")

##### En Python

        formsList = doc.Forms("myFormDocument")
        form = doc.Forms("myFormDocument", "myForm")

## GetDatabase

Returns an instance of the Database service that allows the execution of SQL commands on the database defined and/or stored in the current Base document

### Sintaxi:

svc.GetDatabase(user: str = '', password: str = ''): svc

### Paràmetres:

user, password: Optional login parameters as strings. The default value for both parameters is an empty string "".

### Exemple:

##### En Basic

        Dim myDoc As Object, myDatabase As Object, ui As Object
        Set ui = CreateScriptService("UI")
        Set myDoc = ui.OpenBaseDocument("C:\Documents\myDb.odb")
        ' User and password are supplied below, if needed
        Set myDatabase = myDoc.GetDatabase()
        '   ... Run queries, SQL statements, ...
        myDatabase.CloseDatabase()
        myDoc.CloseDocument()

##### En Python

        ui = CreateScriptService("UI")
        myDoc = ui.OpenBaseDocument(r"C:\Documents\myDb.odb")
        myDatabase = myDoc.GetDatabase()
        #   … Executeu consultes o expressions SQL…
        myDatabase.CloseDatabase()
        myDoc.CloseDocument()

## IsLoaded

Retorna True si el FormDocument especificat està obert actualment.

### Sintaxi:

svc.IsLoaded(formdocument: str): bool

### Paràmetres:

formdocument: The name of a FormDocument to be checked, as a case-sensitive string.

### Exemple:

##### En Basic

        If Not oDoc.IsLoaded("myFormDocument") Then
            oDoc.OpenFormDocument("myFormDocument")
        End If

##### En Python

        if not doc.IsLoaded("myFormDocument"):
            doc.OpenFormDocument("myFormDocument")

## OpenFormDocument

Opens the specified FormDocument either in normal or in design mode. This method returns a FormDocument service instance corresponding to the specified form document.

If the form document is already open, it is activated without changing its mode.

If the specified form document does not exist, then Nothing is returned.

### Sintaxi:

svc.OpenFormDocument(formdocument: str, designmode: bool = False): svc

### Paràmetres:

formDocument: The name of the FormDocument to be opened, as a case-sensitive string.

designmode: If this argument is True the FormDocument will be opened in design mode.

### Exemple:

##### En Basic

Most form documents are stored in the root of the Base document and they can be opened simply using their names, as in the example below:

        Dim oFormDoc As Object
        oFormDoc = oDoc.OpenFormDocument("myFormDocument")

If form documents are organized in folders, it becomes necessary to include the folder name to specify the form document to be opened, as illustrated in the following example:

        oFormDoc = oDoc.OpenFormDocument("myFolder/myFormDocument")

##### En Python

        formDoc = doc.OpenFormDocument("myFormDocument")

        formDoc = doc.OpenFormDocument("myFolder/myFormDocument")

## OpenQuery

Opens the Data View window of the specified query and returns an instance of the Datasheet service.

La consulta es pot obrir en mode normal o de disseny.

If the query is already open, its Data View window will be made active.

Tancar el document del Base farà que també es tanqui la finestra Visualització de dades.

### Sintaxi:

svc.OpenQuery(queryname: str, designmode: bool = False): obj

### Paràmetres:

queryname: The name of an existing query as a case-sensitive String.

designmode: If this argument is set to True the query is opened in design mode. Otherwise it is opened in normal mode (Default = False).

### Exemple:

##### En Basic

          oDoc.OpenQuery("MyQuery", DesignMode := True)

##### En Python

          doc.OpenQuery("MyQuery", designmode=True)

## OpenTable

Opens the Data View window of the specified table and returns an instance of the Datasheet service.

La taula es pot obrir tant en mode normal com en mode de disseny.

Si la taula ja està oberta, la finestra de vista de dades s'activarà.

Tancar el document del Base farà que també es tanqui la finestra Visualització de dades.

### Sintaxi:

svc.OpenTable(tablename: str, designmode: bool = False): obj

### Paràmetres:

tablename: The name of an existing table as a case-sensitive String.

designmode: If this argument is set to True the table is opened in design mode. Otherwise it is opened in normal mode (Default = False).

### Exemple:

##### En Basic

          oDoc.OpenTable("MyTable", DesignMode = False)

##### En Python

          doc.OpenTable("MyTable", designmode=False)

## PrintOut

Aquest mètode envia el contingut del document de formulari donat a una impressora per defecte o a una impressora definida pel mètode SetPrinter().

Returns True if the document was successfully printed.

The method PrintOut is deprecated since LibreOffice 7.6. Although it is still available, it may be removed from the Base service in a future release. Use the PrintOut method from the FormDocument service instead.

### Sintaxi:

svc.PrintOut(opt formdocument: str, pages: str = "", copies: num = 1): bool

### Paràmetres:

formdocument: A valid document form name as a case-sensitive string. The form document must be open. It is activated by the method.

pages: The pages to print as a string, like in the user interface. Example: "1-4;10;15-18". Default is all pages.

copies: The number of copies. Default is 1.

### Exemple:

##### En Basic

        If oDoc.PrintOut("myForm", "1-4;10;15-18", Copies := 2) Then
            ' ...
        End If

##### En Python

        if doc.PrintOut('myForm', copies=3, pages='45-88'):
            # ...

## SetPrinter

Define the printer options for a form document. The form document must be open.

Returns True when successful.

The method SetPrinter is deprecated since LibreOffice 7.6. Although it is still available, it may be removed from the Base service in a future release. Use the SetPrinter method from the Document service instead.

### Sintaxi:

svc.SetPrinter(opt formdocument: str, opt printer: str, opt orientation: str, paperformat: str): bool

### Paràmetres:

formdocument: A valid document form name as a case-sensitive string.

printer: The name of the printer queue where to print to. When absent, the default printer is set.

orientation: Either PORTRAIT or LANDSCAPE. When absent, left unchanged with respect to the printer settings.

paperformat: Specifies the paper format as a string value that can be either A3, A4, A5, LETTER, LEGAL or TABLOID. Left unchanged when absent.

### Exemple:

##### En Basic

        oDoc.SetPrinter("myForm", Orientation := "PORTRAIT")

##### En Python

        doc.SetPrinter('myForm', paperformat='TABLOID')

Totes les rutines o identificadors bàsics de ScriptForge que tenen el prefix de guió baix "_" estan reservats per a ús intern. No estan pensats per utilitzar-los en macros de Basic o scripts de Python.

Temes relacionats

SFDatabases.Database service

SFDocuments.Document service

SFDocuments.Form service

ScriptForge.UI service

Ens cal la vostra ajuda!