Using BASIC to report on all the properties and methods of a database object

Can anyone help on using BASIC to report on all the properties and methods of a database object?

I’ve tried two ways, one suggested by ChatGPT and the other from the LibreOffice help web site - the section “SFDocuments.Document service”.

You can see below for the results
1 ThisDatabaseDocument isn’t recognised
2, DocumnentProperties isn’t recognised.

I’m hoping that this is just a user error in that I haven’t got some vital library or service loaded but can anyone advise?

Thanks

Nick

1, REM code from chatGPT
Sub DisplayPropertiesOfThisDatabaseDocument()
Dim i As Integer
Dim properties() As String

’ Get the properties of ThisDatabaseDocument
properties = ThisDatabaseDocument.Properties.getPropertyNames() 'Line throwing the error message

' Display each property
For i = LBound(properties) To UBound(properties)
   MsgBox "Property name: " & properties(i) & vbCrLf & _
            "Value: " & ThisDatabaseDocument.Properties.getPropertyValue(properties(i))
 Next i

End Sub

When I try to use this code I get the error message at the line “properties = ThisDatabaseDocument.Properties.getPropertyNames()”

 The error number raised is 	12
 The error description is 		"Variable not defined.
 					Additional information: ThisDatabaseDocument"

2, I also tried using the code noted at SFDocuments.Document service
Sub LO_BASIC_example_to_show_all_doc_properties()
REM The code noted at SFDocuments.Document service
GlobalScope.BasicLibraries.loadLibrary(“ScriptForge”)
Dim ui as Variant : Set ui = CreateScriptService(“UI”)
Dim oDoc as Object
Set oDoc = ui.OpenDocument(ThisComponent.URL)
Dim propDict as Object
Set propDict = oDoc.DocumentProperties ’ Line throwing the error message
Dim keys as Variant : propKeys = propDict.Keys
Dim k as String, strProp as String
For Each k In propKeys
strProp = strProp & k & ": " & propDict.Item(k) & CHR$(10)
Next k
MsgBox strProp
end sub

This fails at the line “Set propDict = oDoc.DocumentProperties” with the message and the property or method “DocumentProperties” isn’t recognised.

3, LibreOffice environment
Version: 7.6.6.3 (X86_64) / LibreOffice Community
Build ID: d97b2716a9a4a2ce1391dee1765565ea469b0ae7
CPU threads: 8; OS: Linux 6.5; UI render: default; VCL: gtk3
Locale: en-GB (en_GB.UTF-8); UI: en-US
Calc: threaded

I’m using Xubunutu 22.04.

“The properties, methods or arguments marked with (*) are NOT applicable to Base documents.” is quoted from here:
https://help.libreoffice.org/24.2/en-GB/text/sbasic/shared/03/sf_document.html?&DbPAR=SHARED&System=UNIX

Wanderer,

I understand that different document types will have different properties and macros.

My question is if it’s possible within a BASIC macro to list the properties or methods for a database document.

Thanks

Nick

You can add on local error resume next to skip the problematic properties.

What I’m picking up is that there isn’t a programmatic way of looping through whichever Properties have been set up for a BASE document. Instead I use the list provided in at the reference above and, in effect, hard-code the Properties I want or use “RESUME NEXT” to avoid those I don’t want…

Using that link provided the Properties are noted below with a comment as to whether they can be used for all document types or are not available for BASE databases.

I’m guessing that it’s the same scenario for Methods and I so need to go and find the equivalent list.

Thank you to everyone who contributed.

As far as I’m concerned this tracker can be marked as solved.

Regards

REM List of document properties as noted in SFDocuments.Document service
REM Document Property Scope
REM DocumentType all
REM IsBase all
REM IsCalc all
REM IsDraw all
REM IsFormDocument all
REM IsImpress all
REM IsMath all
REM IsWriter all
REM XComponent all
REM CustomProperties () notdb
REM Description (
) notdb
REM DocumentProperties () notdb
REM ExportFilters (
) notdb
REM FileSystem () notdb
REM ImportFilters (
) notdb
REM Keywords () notdb
REM Readonly (
) notdb
REM StyleFamilies () notdb
REM Subject (
) notdb
REM Title () notdb
REM XDocumentSettings (
) notdb

Maybe check the source of introspection tools MRI and XRay. Both tools show the properties, so “there is a way”…

Just an illustration

1 Like

@Wanderer and JohnSUN, I wasn’t aware of those tools and will have a look.

Thanks

After all the help I received I was able to write some code to extract the information I was after.

If anyone is interested please find below that code (with error trapping and file handling removed).

Regards

Option Explicit
Sub ShowDatabaseElementsCore()
REM Display for a LibreOffice Base document its
REM	Properties
REM	QueryDefinition Names
REM	Table Names
REM Environment
REM Version: 7.6.6.3 (X86_64) / LibreOffice Community
REM Build ID: d97b2716a9a4a2ce1391dee1765565ea469b0ae7
REM CPU threads: 8; OS: Linux 6.5; UI render: default; VCL: gtk3
REM Locale: en-GB (en_GB.UTF-8); UI: en-US
REM Calc: threaded
REM Xubuntu 22.04
REM 30 April 2024

GlobalScope.BasicLibraries.loadLibrary("ScriptForge")

DIM ol_current_ODB as Object
DIM ol_data_source as Object
DIM ol_object_settings as Object
DIM ol_databasedocument as object

DIM sl_object_path as String
DIM sl_data_source_URL  as String
DIM sl_JavaDriverClass as string
DIM sl_JavaDriverClassPath as string 

DIM sl_output_string as string 
DIM sl_output_file_path as string
dim sl_output_file_name as string
dim il_file_number as integer

Dim ol_QueryDefinitions As Object
Dim ol_QueryDefinition As Object
DIM il_object_queries_count as Integer
DIM il_object_queries_subscript as Integer

Dim ol_Connection as object
Dim ol_DataSource_for_Tables as object
DIM ol_Tables() as object
Dim ol_Table as object
DIM il_object_tables_count as Integer
DIM il_object_tables_subscript as Integer

ol_current_ODB = ThisComponent
sl_object_path = ol_current_ODB.URL 
sl_output_file_path = left(sl_object_path ,len(sl_object_path)-4)
sl_output_file_name = sl_output_file_path & "BASEDocElements" & format(now(),"yymmdd") & ".txt"

ol_data_source = ol_current_ODB.DataSource
sl_data_source_URL =  ol_data_source.URL
ol_object_settings = ol_data_source.settings
 
 REM Report object file path and Java Driver Class and Class Path.
MsgBox "ODB file path : " &  chr(9) & sl_object_path, MB_OK + MB_ICONINFORMATION,"ODB file path"
MsgBox "Data Source URL: " &  chr(9) &  sl_data_source_URL, MB_OK + MB_ICONINFORMATION,"DataSource URL"
sl_JavaDriverClass = ol_object_settings.JavaDriverClass
MsgBox "JavaDriverClass: " &  chr(9) & sl_JavaDriverClass, MB_OK + MB_ICONINFORMATION,"JavaDriverClass"

sl_JavaDriverClassPath =  ol_object_settings.JavaDriverClassPath
MsgBox "JavaDriverClassPath: " &  chr(9) &   sl_JavaDriverClassPath, MB_OK + MB_ICONINFORMATION,"JavaDriverClassPath"

REM Report the document properties available to BASE documents. 
ol_databasedocument = CreateScriptService("SFDocuments.Document",ThisComponent)

MsgBox "The value for the database-document property DocumentType is " & ol_databasedocument.DocumentType, MB_OK + MB_ICONINFORMATION,"Document Type"
MsgBox "The value for the database-document property IsBase is " & ol_databasedocument.IsBase, MB_OK + MB_ICONINFORMATION,"IsBase Element"
MsgBox "The value for the database-document property IsCalc is " & ol_databasedocument.IsCalc, MB_OK + MB_ICONINFORMATION,"IsCalc"
MsgBox "The value for the database-document property IsDraw is " & ol_databasedocument.IsDraw , MB_OK + MB_ICONINFORMATION,"IsDraw"
MsgBox "The value for the database-document property IsFormDocument  is " & ol_databasedocument.IsFormDocument , MB_OK + MB_ICONINFORMATION,"IsFormDocument"
MsgBox  "The value for the database-document property IsImpress is " & ol_databasedocument.IsImpress, MB_OK + MB_ICONINFORMATION,"IsImpress Element" 
MsgBox  "The value for the database-document property IsMath is " & ol_databasedocument.IsMath, MB_OK + MB_ICONINFORMATION,"IsMath"
MsgBox  "The value for the database-document property IsWriter is " & ol_databasedocument.IsWriter, MB_OK + MB_ICONINFORMATION,"IsWriter"

REM Report the BASE document QueryDefintion Names. 
 
ol_QueryDefinitions = ol_data_source.getQueryDefinitions()
 
il_object_queries_subscript = 0
For  il_object_queries_subscript = 0 To ol_QueryDefinitions.Count() - 1
     ol_QueryDefinition = ol_QueryDefinitions( il_object_queries_subscript)
     MsgBox "Value for the database-document QueryDef("&format(il_object_queries_subscript)&")  is " & ol_QueryDefinition.Name, MB_OK + MB_ICONINFORMATION,"Query Definition"
Next 
REM Report the BASE document Table Names. 
ol_Connection = ol_data_source.GetConnection("","")
ol_Tables = ol_Connection.getTables(null, "","%",NULL)
il_object_tables_subscript = 0
For  il_object_tables_subscript = 0 To ol_Tables.Count() - 1
     ol_Table = ol_Tables(il_object_tables_subscript)
     MsgBox "Value for the database-document Table("&format(il_object_tables_subscript)&")  is " & ol_Table.Name, MB_OK + MB_ICONINFORMATION,"Table"
Next il_object_tables_subscript

MsgBox "Macro ""ShowDatabaseElements"" finished successfully at " & format(now(),"yyyy/mm/dd hh:mm"), MB_OK + MB_ICONINFORMATION,"Macro finished successfully"

End Sub