iCoffee
iCoffee

Reputation: 255

embed a button into an excel chart

Roughly speaking, I want to create a child class from Excel Chart to add the following features into the child:

  1. add a button to the visual appearance of the chart (the button should have a fixed position relatively to the chart - so if I move chart, the button moves as well). Actually, it doesn't have to be exactly button - it may be anything else which allows clicking on it (or do any other action) and do some activities after the click.
  2. add some additional properties to the chart (these properties doesn't appear vizually but they influence the representation of the time-series). Maybe there is a standard way to customize the set of the properties of Excel Chart?

I don't know to what extent it is possible. Maybe "child class" is not that good idea to serve such simple needs. Maybe VSTO has something, but I can't find anything suitable in the Internet.

Any help will be very much appreciated!

PS

For the subquestion #2 I've decided to use the field TAG to put there an object with additional parameters.

Upvotes: 0

Views: 362

Answers (1)

Andy G
Andy G

Reputation: 19367

In answer to your first question it is possible to embed a form-control button or a shape in a chart, and have it keep its position relative to the chart area, by cutting and pasting it onto the chart. Use the OnAction property to assign a procedure to the button.

Recorded code (in Excel) is like this:

Sub Macro1()
    ActiveSheet.Buttons.Add(166.5, 48, 48, 32.25).Select
    Selection.OnAction = ActiveWorkbook.Name & "!TestMacro"
    Selection.Cut
    ActiveSheet.ChartObjects("Chart 1").Activate
    ActiveChart.Paste
    Selection.ShapeRange.IncrementLeft 10
    Selection.ShapeRange.IncrementTop 30
End Sub

It may be possibly to do this programmatically without cutting and pasting, perhaps by adding to the ChartObject's ShapeRange, but I haven't explored this.

I do not know to what extent you can directly subclass an Excel Chart (if at all) and suspect that you might just create a wrapper class for it in C#.

Upvotes: 2

Related Questions