Here is the error message:
Debug Error 2: The current document is not recognized as a LibreOffice Base form or report.
Please ensure the form is fully opened in ‘Form View’ and is part of a Base database.
Detected document type (implementation name): SwXTextDocument
Here is the code in the macro:
"Sub OpenFormByName(oEvent As Object)
’ This macro opens a specified form in LibreOffice Base.
’ It can be assigned to a button’s “Execute action” event.
’ This code is compatible with LibreOffice version 25.2.4.3 on macOS Sequoia 15.5.
Dim oDatabaseContext As Object
Dim oDataSource As Object
Dim sFormName As String
Dim oForm As Object
Dim oForms As Object
Dim oCurrentDoc As Object ' Represents the active document
Dim oCurrentController As Object ' Represents the active view of the document
Dim sDocImplementationName As String ' For debugging the document type
' --- Configuration ---
' IMPORTANT: Replace "YourFormNameHere" with the actual name of your form.
' For example, if your form is named "Customers", change this to sFormName = "Customers"
sFormName = "MyNativeForm" ' e.g., "CustomersForm", "OrdersEntry"
' ---------------------
On Error GoTo ErrorHandler
' --- Attempt to get the current document more robustly ---
' This approach tries to get the document from the current desktop frame,
' which can sometimes resolve context issues with ThisComponent.
If Not IsNull(StarDesktop.getCurrentFrame()) Then
If Not IsNull(StarDesktop.getCurrentFrame().getController()) Then
oCurrentDoc = StarDesktop.getCurrentFrame().getController().getModel()
End If
End If
' --- Critical Context Checks ---
' These checks are designed to pinpoint why the document might not be recognized as a Base form.
' Check 1: Is oCurrentDoc Null?
If IsNull(oCurrentDoc) Then
MsgBox "Debug Error 1: The active document (oCurrentDoc) is Null. The macro could not determine the current document context.", _
16, "Macro Context Error"
Exit Sub
End If
' Get the implementation name for debugging purposes
On Error Resume Next ' Temporarily disable error handling for this specific property access
sDocImplementationName = oCurrentDoc.getImplementationName()
On Error GoTo ErrorHandler ' Re-enable error handling
' Check 2: Does oCurrentDoc have the expected database document interface?
' A LibreOffice Base form is part of an XOfficeDatabaseDocument.
If Not HasUnoInterfaces(oCurrentDoc, "com.sun.star.sdb.XOfficeDatabaseDocument") Then
MsgBox "Debug Error 2: The current document is not recognized as a LibreOffice Base form or report." & Chr(13) & _
"Please ensure the form is fully opened in 'Form View' and is part of a Base database." & Chr(13) & _
"Detected document type (implementation name): " & sDocImplementationName, _
16, "Macro Context Error"
Exit Sub
End If
' Check 3: Is the document's controller active (meaning the form is fully open and visible)?
oCurrentController = oCurrentDoc.getCurrentController()
If IsNull(oCurrentController) Or Not oCurrentController.isConnected() Then
MsgBox "Debug Error 3: The document's controller is not active or connected." & Chr(13) & _
"Please ensure the form is fully loaded and visible in 'Form View' before clicking the button.", _
16, "Macro Context Error"
Exit Sub
End If
' Check 4: Is the DataSourceName available and valid for the current form?
If IsNull(oCurrentDoc.DataSourceName) Or oCurrentDoc.DataSourceName = "" Then
MsgBox "Debug Error 4: The current form is not linked to a data source." & Chr(13) & _
"Please ensure your form is saved and properly associated with a database.", _
16, "Macro Debugging Info"
Exit Sub
End If
' --- End Critical Context Checks ---
' Get the database context service
oDatabaseContext = CreateUnoService("com.sun.star.sdb.DatabaseContext")
If IsNull(oDatabaseContext) Then
MsgBox "Debug Error 5: Could not create DatabaseContext service. LibreOffice installation issue?", _
16, "Macro Debugging Info"
Exit Sub
End If
' Get the specific data source by name
On Error GoTo DataSourceErrorHandler ' Temporarily switch error handler for this specific step
oDataSource = oDatabaseContext.getByName(oCurrentDoc.DataSourceName)
On Error GoTo ErrorHandler ' Switch back to general error handler
If IsNull(oDataSource) Then
MsgBox "Debug Error 6: Data source '" & oCurrentDoc.DataSourceName & "' not found or accessible." & Chr(13) & _
"Please ensure the database is registered and the name is correct.", 16, "Macro Debugging Info"
Exit Sub
End If
' Ensure the data source is connected
If Not oDataSource.IsConnected Then
oDataSource.connect()
End If
' Get the forms collection from the database document
If IsNull(oCurrentDoc.Forms) Then
MsgBox "Debug Error 7: Could not access the Forms collection from the current document." & Chr(13) & _
"Ensure this macro is called from a form document.", 16, "Macro Debugging Info"
Exit Sub
End If
oForms = oCurrentDoc.Forms
' Check if the target form exists and open it
If oForms.hasByName(sFormName) Then
oForm = oForms.getByName(sFormName)
oForm.open()
Else
MsgBox "Error: Form '" & sFormName & "' not found in the database." & Chr(13) & _
"Please verify the form name in the macro code ('" & sFormName & "') matches an existing form.", _
16, "Form Not Found"
End If
Exit Sub
DataSourceErrorHandler:
MsgBox "An error occurred while trying to get the data source: " & Err.Description & Chr(13) & _
"Error Number: " & Err.Number & Chr(13) & _
“This often happens if the database is not properly registered or the form is not saved/linked.”, _
16, “Macro Error - Data Source”
Exit Sub
ErrorHandler:
MsgBox “A BASIC runtime error occurred:” & Chr(13) & _
"Error Description: " & Err.Description & Chr(13) & _
"Error Number: " & Err.Number & Chr(13) & _
“If you are seeing this, it means one of the specific debug checks above did not trigger, " & _
“but a general error occurred later. Please double-check macro assignment and security.”, _
16, “Macro Execution Error”
End Sub”
The form is an internal form created using the Form Wizard. The macro resides inside the database. I’m working with hsqldb.