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