In my Apache OpenOffice Calc document, comprising three sheets named Sheet1, Sheet2, and Sheet3, I aim to extract data exclusively from "Sheet1" within the range "A1:F20." Subsequently, I intend to save this extracted data as "File.xls" in the directory "D:\File" using OpenOffice Basic. Could you please provide the appropriate code for this task?
I have used the below code:
Code: Select all
Sub Macro1
Call UnlockRangeInObservationSheet
' ----------------------------------------------------------------------
' Define variables
Dim document As Object
Dim dispatcher As Object
' ----------------------------------------------------------------------
' Get access to the document
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
' ----------------------------------------------------------------------
' Set up parameters to navigate to a specific cell range in "Observation_Sheet"
Dim args1(0) As New com.sun.star.beans.PropertyValue
args1(0).Name = "ToPoint"
args1(0).Value = "'Observation_Sheet'.$A$1:$F$20"
'dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, args1())
' ----------------------------------------------------------------------
' Set up parameters to export "Observation_Sheet" to an Excel file
Dim args2(8) As New com.sun.star.beans.PropertyValue
Dim Cel As Object
Dim Werkblad As Object
Dim Werkmap As Object
Werkmap = ThisComponent
Werkblad = Werkmap.Sheets.getByName("Tracker")
Cel = Werkblad.getCellByPosition(0, 0) 'A9
Path = ThisComponent.Sheets.getByName("Tracker").getCellByPosition(0, 1).String 'J5
If Path = "" Then
MsgBox "Please mention Sanction Date"
Else
args2(0).Name = "URL"
args2(0).Value = Path & Cel.getString() & ".xls"
args2(1).Name = "FilterName"
args2(1).Value = "MS Excel 97"
args2(2).Name = "FilterOptions"
args2(2).Value = "MS Excel 97"
args2(3).Name = "Overwrite"
args2(3).Value = True
args2(4).Name = "SelectedSheets"
args2(4).Value = True ' Export only selected sheets
' Change the property from "Selection" to "Table"
args2(5).Name = "Table"
Dim sheetList(0) As Variant
sheetList(0) = "Observation_Sheet" ' Specify the sheet to export
args2(5).Value = sheetList
dispatcher.executeDispatch(document, ".uno:SaveAs", "", 0, args2())
' ----------------------------------------------------------------------
' Close the exported document in the background
Dim exportedDocument As Object
exportedDocument = StarDesktop.loadComponentFromURL(args2(0).Value, "_blank", 0, Array())
exportedDocument.close(True)
End If
Call LockRangeInObservationSheet
End Sub
But it is working as following:
1. It extract all the sheets
2. It closes the current file and opened the new file
What need to be done. If possible reply as soon as possible.
Thank You
Sourav Sarkar