How to precise a selected area in libreoffice sheet to be printed in pdf

I use the following code see below. I would like to select a given area in the sheet, this area should be put a pdf file. I would like also to give a precise name to the pdf file (not the name of the sheet).

How shall I do that? Many thank in advance.

Best regards

Paul M

Sub ExportActiveSheetPrintRangeToPDF

'Get Active Sheet
   dim document   as object
   dim dispatcher as object
   dim oSheet as object

   document   = ThisComponent.CurrentController.Frame
   dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
   oSheet = thiscomponent.getcurrentcontroller.activesheet
   
'Get Print Range - if no print range has been defined for the Active Sheet, then it will export the entire Active Sheet
   dim aFilterData(0) as new com.sun.star.beans.PropertyValue
   aFilterData(0).Name = "Selection"
   aFilterData(0).Value = oSheet

'Export to PDF
   dim args1(1) as new com.sun.star.beans.PropertyValue
   args1(0).Name = "FilterName"
   args1(0).Value = "calc_pdf_Export"
   args1(1).Name = "FilterData"
   args1(1).Value = aFilterData()

   dispatcher.executeDispatch(document, ".uno:ExportDirectToPDF", "", 0, args1())

'File Done (added this next line so that user knows when file is complete)
   Beep

End Sub

Hello @Paul M,

To export a given range from your sheet to a new PDF file with a given name, you could use the following macro:

Sub CellRangeToPDF( strCellRangeName As String, strURL As String )
REM Exports a specified cell range from the current Calc sheet to a new PDF file.
REM Adapted from Axel Richter: libreoffice-calc-macro-how-to-export-cell-range-to-pdf-file. 
REM <strCellRangeName>:	the address of the range to export e.g. "A1:F9".
REM						pass an empty string to export the current selection.
REM <strURL>:	file path to export the range to, e.g. "/home/user/Desktop/myCellRange.pdf"
On Local Error GoTo Exit_Sub	REM Fails quietly on Error. 

	Dim oDoc As Object, oSheet As Object, oCellRange As Object
	oDoc   = ThisComponent
	oSheet = oDoc.getCurrentController().getActiveSheet()
	
	If strCellRangeName = "" Then
		oCellRange = oDoc.getCurrentSelection()
	Else
		oCellRange = oSheet.getCellRangeByName( strCellRangeName )	REM causes an exception for invalid Range names.
	End If
	
	Dim aFilterData(0) As New com.sun.star.beans.PropertyValue
	aFilterData(0).Name = "Selection"
	aFilterData(0).Value = oCellRange
	
	Dim aMediaDescriptor(2) As New com.sun.star.beans.PropertyValue
	aMediaDescriptor(0).Name = "FilterName"
	aMediaDescriptor(0).Value = "calc_pdf_Export"
	aMediaDescriptor(1).Name = "FilterData"
	aMediaDescriptor(1).Value = aFilterData()
	
	strURL = ConvertToURL( strURL )
	oDoc.storeToURL( strURL, aMediaDescriptor() )	REM causes an exception for invalid URLs.
Exit_Sub:
End Sub

Thanks very much for the help. It works very well even in openoffice!