# In AppleScript with Excel - How To Get Selected Chart

The following AppleScript code sets the size of the last chart added to a sheet in Excel - to 30cm wide and 20cm tall. (I experimented with the numerical values but I don’t understand them.)

``````tell application "Microsoft Excel"
tell active sheet
tell last chart object
set height to 567
set width to 850.5
end tell
end tell
end tell
``````

This was tough as there’s not much on the web about this.

I have a question, though:

How to I get the selected chart, not just the last one added? (Usually the last one added is the one I want to resize but I’d rather not assume that.)

(The above script will be run from Keyboard Maestro, and probably triggered by a button in Metagrid.)

I can’t answer the chart question, but here’s the explanation for the numbers…

Related to Dots Per Inch…

30 cm * (0.393701 inches / cm) * 72 DPI = 850.4
20 cm * (0.393701 inches / cm) * 72 DPI = 566.9

1 Like

Thanks! I wondered about DPI but time was too fleeting to do the maths. At least that means I shouldn’t worry about the numbers needing to change.

I don’t have Excel on the Mac, only my work PC; but the chart section of this old documentation would seem to suggest `active chart` would do it. I just can’t test it to confirm.

1 Like

That would be a useful document, and I have it already.

`active chart` doesn’t get me very far as apparently it’s the chart object (sic) I can set height and width for, not a chart. In my code I have `last chart object`. I apparently (I’ve tested it) can’t set the height or width for `active chart` nor find the associated chart object.

You need the `chart area object` from the `active chart`.

``````set ch to chart area object of active chart
set height of ch to 200.0
set width of ch to 400.0
``````
2 Likes

Thank you! I’m finding the Excel Applescript data model fiddly in the extreme.

(There are a few tricks with it I may well put into a blog post - as finding stuff on the web has proven difficult. I hope you don’t mind if I include this.)