Can I Update a Chart Embedded in Writer Using a Macro

So I have a chart embedded in Writer that I created from a TextTable using Insert → Chart.

I find that I can update this chart by double clicking on the chart or right clicking on it and selecting Edit. (For some reason, I find that Tools → Update → Charts does not work for me, even if I record it as a Macro.)

What I want to do is update this embedded chart from a Macro. I’ve tried a number of ideas, but none of them work, or do not work properly. Assume that the chart is in object1. Here is a snippet of code I’ve tried:

Sub UpdateChart()
    
        Dim xObj, oChart As Object
        
          If ThisComponent.EmbeddedObjects.hasByName("Object1") Then
           xObj = ThisComponent.EmbeddedObjects.getByName("Object1")
           oChart = xObj.getEmbeddedObject()
           oChart.update()
          End If

End Sub

That fails to do anything. Another try was:

Sub UpdateChart()

    Dim xObj, oChart As Object

      If ThisComponent.EmbeddedObjects.hasByName("Object1") Then
        xObj = ThisComponent.EmbeddedObjects.getByName("Object1")
        XControl = xObj.getExtendedControlOverEmbeddedObject()
        XControl.doVerb(0)
     End If

End Sub

This effectively replicates the user right-clicking on the Chart and selecting Edit. So it puts the Chart in Edit mode and updates it as well. But – this leaves the Chart locked in Edit mode so far as Macro code goes and makes it hard to do anything else at that point.

My last try came about as a desperate attempt to just plug other numbers into the doVerb() to see if I could get it to do anything else. What I found was that I could update the chart in Edit mode, then escape the Edit mode by using doVerb(-3). But – that completely messed up the SideBar.

Sub UpdateChart()

    Dim xObj, oChart As Object

      If ThisComponent.EmbeddedObjects.hasByName("Object1") Then
        xObj = ThisComponent.EmbeddedObjects.getByName("Object1")
        XControl = xObj.getExtendedControlOverEmbeddedObject()
        XControl.doVerb(0)
        XControl.doVerb(-3)
     End If

End Sub

Here’s what you see:

So the chart was updated, but the SiderBar is now empty, an undesirable side effect.

Does anyone know how a chart embedded in Writer can be updated from a Macro?

… I cannot reproduce that. When I change a value in the table, the chart is updated automatically

Thanks for the reply.

Actually, I can usually update the chart by right-clicking on it and selecting Edit. Sometimes I have to do this a number of times before it takes.

But what I really want to do is put a pie chart in a hidden section in Writer, then change its data by a macro, then update the chart, then copy / paste it elsewhere in the document as an image, all by a macro. I’ve got most of the code worked out, it’s just that last step – updating the chart.

The third example above works, but it plays havoc with the sidebar, first clearing it, then leaving it that way. (Probably the result of Writer trying to change the sidebar to display edit options for the chart, but getting hung up when my code instantly executes the doVerb(-3). Just a guess.)

I had thought of experimenting with a Calc sheet embedded in Writer and then seeing if I can create a pie chart from scratch using the same kind of code available in Calc. But would that work in Wrtier?

Just a note. doVerb(-3) returns you to previous cursor position but UI is not visible… This is likely why sidebar is empty. See > LibreOffice: com::sun::star::embed::EmbedVerbs Constant Group Reference
If you leave off that and only have doVerb(0) then the chart is selected and the sidebar shows Chart Type.

You can use the dispatcher to update the chart.

	dim oDoc as variant: oDoc = ThisComponent.CurrentController.Frame
	dim aDispatcher as variant
	aDispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
	aDispatcher.executeDispatch(oDoc, ".uno:UpdateCharts", "", 0, Array())