Detecting a Selected Shape Object in Excel


(Buzz Hill) #1

I am trying to detect a rectangular shape (Bar) in Excel. Excel has a Selection property, but it only works for cells. So, if a user has modified a Bar, I need to know which one to act on.

(Jonas Whale) #2

I’m not sure to understand what’s your goal…

In Excel (as far as I know) there’s no way to determine if a shape is selected.
Here is a script you can get inspired of:

 tell application id "XCEL"
	set shapeList to (shapes of sheet 1 whose has chart = true)
	if (count of shapeList) = 0 then
		error "There is no chart in this document."
	else if (count of shapeList) = 1 then
		set theShape to (item 1 of shapeList)
		set shapeNames to name of shapes of sheet 1 whose has chart = true
		set chosenShape to choose from list shapeNames without multiple selections allowed
		if chosenShape = false then return
		set shapeList to (shapes of sheet 1 whose name = chosenShape)
		set theShape to (item 1 of shapeList)
	end if
end tell

(Buzz Hill) #3

Thanks for your effort. Unfortunately, the shapes have no chart.

Ironically, VBA knows which shapes are selected. I can use VBA to put the name of the shape somewhere on the spreadsheet, then my script can retrieve the name and then do something with the shape. So, I can have my script run the macro, then retrieve the name of the shape.

Thanks again.