Calc Basic select a chart

It seems that folks are going down the wrong path so I’ll simplify.

When I draw a chart in calc basic I would like to select it using a basic command.

ThisComponent.CurrentController.Select() works with cells and ranges but not charts.

What command will select a chart?

Thanks, Mike

PS: The last suggestion of using Ctrl+Z I can’t test because at the moment trying to draw a chart crashes LO to the point that I must restart the GUI to even try calc again.
MM

Select first shape on active sheet and with Tab key jump to next:

Sub SelectFirstShapeOnActiveSheet
Dim oCurrentController As Variant, oActiveSheet As Variant, oDrawPage As Variant
	oCurrentController = ThisComponent.getCurrentController()
	oActiveSheet = oCurrentController.getActiveSheet()
	oDrawPage = oActiveSheet.getDrawPage()
	If oDrawPage.hasElements() Then
		oCurrentController.Select(oDrawPage.getByIndex(0))
	EndIf
End Sub

Hi JohnSUN,

I copy/pasted your code and put a call to it right after drawing the chart but it isn’t selected, still need to select with the mouse in order to delete the graph.

Where should I look to see why something that works for you doesn’t for me?

Thanks,
Mike

For some reason unbeknownst to me, neither of the offered suggestions worked for me.

Perhaps it’s just as well since selecting the chart with the mouse then deleting it may well have been what was causing calc to crash.

Occasionally I would see this error message, “C++ code threw St9bad_alloc: std::bad_alloc”.

Since I figured out how to delete the chart using oCharts.removeByName(), I’ve quit having crashes. Perhaps improperly disposing the chart was creating memory leaks unfortunately basic has no way to test that assumption that I know of.

None the less, I want to say thank you for the assistance offered.

Be Well,
Mike

Hi, @MikeMcClain46

Sub DeleteFirstImageInserted
ThisComponent.getCurrentController().getActiveSheet().getDrawPage() _
.remove( ThisComponent.getCurrentController().getActiveSheet().getDrawPage().getByIndex(0))
'The (0) represents the first image inserted.
End Sub

If there is more than one image in the spreadsheet, adjust the macro for the correct image.

Thank you for that Sir.

There is also Charts.RemoveByName() and I wonder if there is a reason to prefer one over the other.

However that’s for later, my current challenge as stated initially is how to select the chart in Basic so that after creating and viewing it all I need do is hit Delete to get it off the screen.

Thanks for your input.

Mike

after creating and viewing it all I need do is hit Delete to get it off the screen

Oh, remove LAST CREATED chart? What about deselect chart (click anywhere) and press Ctrl+Z?

Mr. Schiavinatto,
I didn’t realize until much later that Buttons are images and your solution deleted a couple of buttons off my page.
Again thanks for your input.
Mike