How to Export cell range to images

Hi I’m moving from Excel macro
firstly Excel is also not having a direct option to export cell range into image file.
so I’m using work-around method by create empty chart object and then paste cell range image into the chart.
and then now I can export chart as image file.

now I can’t run the same code with LibreOffice Calc
any expert here can help me out please…

this code works Thank you JohnSUN
but still problem with pop-up window asking for jpg option.
how to disable it ?

rem ----------------------------------------------------------------------
 dim args1(0) as new com.sun.star.beans.PropertyValue
 args1(0).Name = "ToPoint"
 args1(0).Value = "$B$2:$F$40"

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

 rem ----------------------------------------------------------------------
 dim args2(3) as new com.sun.star.beans.PropertyValue
 args2(0).Name = "URL"
 args2(0).Value = "file:///C:/Users/Desktop/Doc/picture/image.jpg"
 args2(1).Name = "FilterName"
 args2(1).Value = "calc_jpg_Export"
 args2(2).Name = "FilterData"
 args2(2).Value = Array(Array("ColorMode",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("Quality",0,100,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("PixelWidth",0,794,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("PixelHeight",0,1123,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("LogicalWidth",0,21005,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("LogicalHeight",0,29708,com.sun.star.beans.PropertyState.DIRECT_VALUE))
 args2(3).Name = "SelectionOnly"
 args2(3).Value = true

 dispatcher.executeDispatch(document, ".uno:ExportTo", "", 0, args2())

For python users Live LibreOffice Python UNO Examples has Export Calc Sheet Range as Image example that does this in python.

Hello and welcome!

Just try to record the sequence File - Export… - File type - Selection:ON with the macro recorder. This will give you almost ready-to-use code that you just need to change a little - remove the dispatcher and replace its .ExportTo command with the usual storeToUrl spreadsheet method.

For example, for export to PNG, I got the following:

Sub ExportSelectionToPNGImage(sFileName As String)
Dim args(2) as new com.sun.star.beans.PropertyValue
	args(0).Name = "FilterName"
	args(0).Value = "calc_png_Export"
	args(1).Name = "FilterData"
	args(1).Value = Array(Array("Compression",0,6,com.sun.star.beans.PropertyState.DIRECT_VALUE), _
			Array("Interlaced",0,1,com.sun.star.beans.PropertyState.DIRECT_VALUE), _
			Array("Translucent",0,1,com.sun.star.beans.PropertyState.DIRECT_VALUE), _
			Array("PixelWidth",0,256,com.sun.star.beans.PropertyState.DIRECT_VALUE), _
			Array("PixelHeight",0,194,com.sun.star.beans.PropertyState.DIRECT_VALUE), _
			Array("LogicalWidth",0,6772,com.sun.star.beans.PropertyState.DIRECT_VALUE), _
			Array("LogicalHeight",0,5132,com.sun.star.beans.PropertyState.DIRECT_VALUE))
	args(2).Name = "SelectionOnly"
	args(2).Value = True
	
	ThisComponent.storeToUrl(ConvertToURL(sFileName), args)
End  Sub 

Call it with

Sub ExportToFile
	Call ExportSelectionToPNGImage("C:\Users\Desktop\Doc\picture\image_" _
          & FORMAT(NOW(),"MMSS")&".png")
End  Sub

Update Code from your question can be like this:

Sub Export_B2_F40_To_JPG()
Const PICTURE_PREFIX = "C:\Users\Desktop\Doc\picture\image_"
Dim sNewFileName As String 
Dim oCurrentController As Variant
Dim oRangeToExport As Variant
Rem First select range $B$2:$F$40 on active sheet:
	oCurrentController = ThisComponent.getCurrentController()
	oRangeToExport = oCurrentController.getActiveSheet().getCellRangeByName("$B$2:$F$40")
	oCurrentController.Select(oRangeToExport)
Rem Set name for output file
	sNewFileName = PICTURE_PREFIX & FORMAT(NOW(),"MMSS")&".jpg"
Rem Set parameters of export
Dim Args(2) As New com.sun.star.beans.PropertyValue
	Args(0).Name = "FilterName"
	Args(0).Value = "calc_jpg_Export"
	Args(1).Name = "FilterData"
	Args(1).Value = Array( _
		Array("ColorMode",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE), _
		Array("Quality",0,100,com.sun.star.beans.PropertyState.DIRECT_VALUE), _
		Array("PixelWidth",0,794,com.sun.star.beans.PropertyState.DIRECT_VALUE), _
		Array("PixelHeight",0,1123,com.sun.star.beans.PropertyState.DIRECT_VALUE), _
		Array("LogicalWidth",0,21005,com.sun.star.beans.PropertyState.DIRECT_VALUE), _
		Array("LogicalHeight",0,29708,com.sun.star.beans.PropertyState.DIRECT_VALUE))
	Args(2).Name = "SelectionOnly"
	Args(2).Value = True
Rem Store picture
 	ThisComponent.storeToUrl(ConvertToURL(sNewFileName), Args)
End Sub

This works !
But there is still pop-up asking about jpg option / png option
Any command to automaticaly answer this or any option to disable this pop-up ?

Can you click “Edit” under your original question and add the code that you got to your question? It’s hard to talk about the reasons for the strange behavior of a program that is hidden.

done
please help with the next problem as I said how to disable JPG Option window so the next command can continue without me pressing OK.

Yes, in this form, the code calls an additional window of parameters. That is why I wrote about the need for ***“remove the dispatcher and replace its .uno: ExportTo…”***After the macro recorder, I got exactly the same code. See how I modified it