Copy and Paste Chart from Multiple Calc documents to a Writer document

Hello, I’m Trying to copy and paste charts from calc to a writer. The probleme is that only the graph of the first calc document is paste in my writer file. I Think the problem is that i can not precisly point where the graphique should be paste in the writer file. I tried to create a marketText “Graphic_PlaceHolder” where the graph should be paste but it is not working. Can someone know how to do it? Here is the code of my function. I’m willing to create maybe a test file if needed to solve my question:

Thanks

Sub CopyAndPasteChart(oCalcDoc As Object, oDoc As Object, chartName As String)

oCursor.gotoEnd(False) 
' Selection du graphique pour une copie
Dim oController As Object
Dim oPage As Object
oController = oCalcDoc.getCurrentController()
'oActiveSheet = oController.getActiveSheet()
'oDrawPage = oActiveSheet.getDrawPage()
oPage = oCalcDoc.DrawPages.getByIndex(0)
'If oPage.hasByName(chartName) Then
oChart = oPage.getByIndex(0)
oController.select(oChart)

' DispatchHelper pour copier le graphique
Dim oDispatcher As Object
oDispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
oDispatcher.executeDispatch(oController.getFrame(), ".uno:Copy", "", 0, Array())

'placer le curseur à la fin du doc writer
oCursor.gotoEnd(False)

' Insérer un marqueur texte là où le graphique sera collé
Dim markerText As String
markerText = "GRAPHIC_PLACEHOLDER"
oDoc.getText().insertString(oCursor, markerText, False)

' Utiliser le  DispatchHelper pour coller le graphique dans le doc writer
Dim oWriterController As Object
oWriterController = oDoc.getCurrentController()
oWriterController.select(markerText)

'Coller le graphique
oDispatcher.executeDispatch(oWriterController.getFrame(), ".uno:Paste", "", 0, Array())
'Activer la mise en page pour voir le résultat
oWriterController.getFrame().getContainerWindow().setFocus() ' Ensure Writer window is focuse

'Ajouter trois lignes après l'insertion de l'image
oText.insertString(oCursor, Chr(13) & Chr(13) & Chr(13) , False)

'Libérer explicitement les objets pour éviter les conflits de ressources
oChart = Nothing
'oLegend = Nothing
'oGraphicExporter = Nothing
oGraphic = Nothing 
oDispatcher = Nothing
'Fermer le tableur
oCalcDoc.close(True)  

MsgBox "Le graphique " & chartName & " a été bien collé."

End Sub

I won’t comment on details, and I don’t assume that a solution to this task is really useful. Moreover I didnt work with “multiple Calc documenmts”, but with a single selected sheet containing more than one chart. (Easily extendable to all sheets of a Calc document.)

Since I used more time for the related considerations than I had intended, I will nonetheless attach the result here:
disask109928_experiments.ods (55.8 KB)

Thank you so much . The lines I was missing were:
Dim writerShape As Object
writerShape = oDoc.CurrentSelection(0)
writerShape.AnchorType = com.sun.star.text.TextContentAnchorType.AS_CHARACTER

like what ? when ?
step by step.

sounds wise :expressionless:

  1. The code first select the chart in the calc file : This part is working fine

oChart = oPage.getByIndex(0)
oController.select(oChart)

  1. Then it uses the dispatcher to copy : This part is also working fine

Dim oDispatcher As Object
oDispatcher = createUnoService(“com.sun.star.frame.DispatchHelper”)
oDispatcher.executeDispatch(oController.getFrame(), “.uno:Copy”, “”, 0, Array())

  1. Now To paste the copied chart to the Writer file is not working correctly cause the chart should be placed in a precise place where the cursor is (near the the string markerText )

Dim markerText As String
markerText = “GRAPHIC_PLACEHOLDER”
oDoc.getText().insertString(oCursor, markerText, False)

Dim oWriterController As Object
oWriterController = oDoc.getCurrentController()
oWriterController.select(markerText)

oDispatcher.executeDispatch(oWriterController.getFrame(), “.uno:Paste”, “”, 0, Array())

Seems like every charts are just copied and paste in the same random place in the writer file ignoring the position of the cursor, The result is that only one chart is in the Writer file in the end.
I will send a test file when I’ll be in the office