I’m working on a LibreOffice macro that needs to copy a table from one Writer document (specified by the user) into the currently open Writer document. The macro successfully opens the source document and identifies the table, but I’m having trouble copying the table content with its formatting intact.
Sub CopyTableFromFileToCurrentDocument()
Dim sSourceFilePath As String ' File path for the source document
Dim sTableName As String ' The name of the table to copy
Dim oSourceDoc As Object ' The source document
Dim oTable As Object ' The table to copy
Dim oTargetDoc As Object ' The currently open target document
Dim oFrame1 As Object ' Frame of the source document
Dim oFrame2 As Object ' Frame of the target document
Dim oDispatcher As Object ' Dispatcher object for clipboard operations
Dim oCursor As Object ' Cursor for the target document
' Prompt user for the file path
sSourceFilePath = InputBox("Please enter the full file path of the source document:", "Source File Path")
If sSourceFilePath = "" Then
MsgBox "File path not provided. Operation cancelled."
Exit Sub
End If
' Prompt user for the table name
sTableName = InputBox("Please enter the name of the table to copy:", "Table Name")
If sTableName = "" Then
MsgBox "Table name not provided. Operation cancelled."
Exit Sub
End If
' Convert the file path to URL format
sSourceFilePath = ConvertToURL(sSourceFilePath)
' Open the source document
oSourceDoc = StarDesktop.loadComponentFromURL(sSourceFilePath, "Sample_Page2", 0, Array())
If IsNull(oSourceDoc) Then
MsgBox "Failed to open the source document."
Exit Sub
End If
' Check if the source document has the table
If NOT oSourceDoc.getTextTables().hasByName(sTableName) Then
MsgBox "Sorry, the source document does not contain table " & sTableName
oSourceDoc.close(True) ' Close the source document without saving
Exit Sub
End If
' Get the table by name from the source document
oTable = oSourceDoc.getTextTables().getByName(sTableName)
' Clear the clipboard to ensure no old data is present
oDispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
oFrame1 = oSourceDoc.CurrentController.Frame
oDispatcher.executeDispatch(oFrame1, ".uno:ClearClipboard", "", 0, Array())
' Select the entire table in the source document
oSourceDoc.CurrentController.select(oTable)
' Use the clipboard to copy the selected table
oDispatcher.executeDispatch(oFrame1, ".uno:Copy", "", 0, Array())
' Get the target document (currently open document)
oTargetDoc = ThisComponent
oCursor = oTargetDoc.getText().createTextCursor()
' Move the cursor to the end of the target document
oCursor.gotoEnd(False)
' Place the view cursor at the end of the target document to paste the copied table
oTargetDoc.CurrentController.select(oCursor)
oFrame2 = oTargetDoc.CurrentController.Frame
oDispatcher.executeDispatch(oFrame2, ".uno:Paste", "", 0, Array())
' Close the source document without saving changes
oSourceDoc.close(True)
' Notify the user that the table has been copied and pasted into the current document
MsgBox "The table '" & sTableName & "' has been copied to the current document with formatting."
End Sub