I need help exporting a chart

Hi all,

This does export something. A down arrow, which is not on the chart, but does occur on the table to its left.

This chart in question appears on the second sheet (starting counting from 1).

What am I doing wrong?

Many thanks,
-T

Sub ExportChart( ChartName As String )
   Dim MsgRtn     as Integer
   Dim shape
   Dim gef
   Dim args1(1) as new com.sun.star.beans.PropertyValue
   Dim args2(1) as new com.sun.star.beans.PropertyValue
   
   dim document   as object
   dim dispatcher as object

   ' change to the Weight & Balance sheet
   document   = ThisComponent.CurrentController.Frame
   dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
   args1(0).Name = "Nr"
   args1(0).Value = 2
   dispatcher.executeDispatch(document, ".uno:JumpToTable", "", 0, args1())

   shape = ThisComponent.getSheets().getByIndex(0).getDrawPage().getByIndex(0)
   args2(0).Name = "URL"
   ' args2(0).Value = "file:///home/asuka/Downloads/foo.png"
   args2(0).Value = "file:///" + ChartName
   args2(0).Value = Replace( args2(0).Value, "\", "/" )
  '  MsgRtn = MsgBox( args2(0).Value, 0, "ExportChart" )

   args2(1).Name = "MimeType"
   args2(1).Value = "image/png"
 
   gef = CreateUnoService("com.sun.star.drawing.GraphicExportFilter")
   gef.setSourceDocument(shape)
   gef.filter(args2)
End Sub

You’re looking at the wrong sheet for one. This was basically answered by @mikekaganski in a comment on this post → “shape” throws and error

Your code says first sheet & question says second sheet. Also, if you have more than one object on the Draw Page, you need to determine by code (a name is usually best) which object you need to retrieve and not just an arbitrary index number.

How? Where are my mistakes?

ThisComponent.getSheets().getByIndex(0) is the first sheet, that should be ThisComponent.getSheets().getByIndex(1)

Additionally, because you have (according to the question) more than one shape on the sheet. The index of the shape is in question:

shape = ThisComponent.getSheets().getByIndex(0).getDrawPage().getByIndex(???)

From DrawPage you can obtain a count of items on the sheet, loop through the items and check each name for the desired object.

Charts do automatically get assigned a name but you can also give it your own name. See this post → Is it possible in calc to name a chart…

Is there some property that I an ask the chart what its name is?

Since you can have many charts on a sheet, how do you know what chart name to retrieve? To get access to charts, get wanted sheet; then getChart() (also from here you can getCount() for index loop); then getByIndex(index number); then getName().

Best to know the name of the chart and place in the macro for comparison on DrawPage.

Got it figured out. It is working beautifully. Thank you for all the help!

Sub ExportChart( ChartName As String )
   Dim MsgRtn     as Integer
   Dim shape
   Dim gef
   Dim args1(1) as new com.sun.star.beans.PropertyValue
   Dim args2(1) as new com.sun.star.beans.PropertyValue
   
   dim document   as object
   dim dispatcher as object

   ' change to the Weight & Balance sheet and address the weights and balance table

   shape = ThisComponent.getSheets().getByIndex(1).getDrawPage().getByIndex("WeightAndBalanceChart")
   '                                                            |                                              ^---  Name of Chart
   '                                                            ^--- 0 = sheet zero, 1= sheet1 etc, regardless if you have renamed them
   '
   args2(0).Name = "URL"
   ' args2(0).Value = "file:///home/asuka/Downloads/foo.png"
   args2(0).Value = "file:///" + ChartName
   args2(0).Value = Replace( args2(0).Value, "\", "/" )
  '  MsgRtn = MsgBox( args2(0).Value, 0, "ExportChart" )

   args2(1).Name = "MimeType"
   args2(1).Value = "image/png"
 
   gef = CreateUnoService("com.sun.star.drawing.GraphicExportFilter")
   gef.setSourceDocument(shape)
   gef.filter(args2)
End Sub