View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Peter T Peter T is offline
external usenet poster
 
Posts: 5,600
Default Creating a new Excel chart type

Hi Heath

I had a feeling you might have forgotten about VBA, which is where of course
you can easily find all the properties and methods of a Chart, and in
particular for your purposes chart events. Work in VBA first and if needs
adapt to VB6 or .Net later.

Create a normal chart sheet, select some values and press F11. Right click
the chart sheet tab and view code. Look at the two drop downs at the top of
the code module.

For a ChartObject on a worksheet you need to create your own Class but not
quite as you have written. Insert a new normal Module and a new Class module
named "Class1" (right click your file name as shown in a panel to the left
in the VBE).

Copy following into the Class1

Public WithEvents cht As Excel.Chart

Private Sub cht_Select(ByVal ElementID As Long, _
ByVal Arg1 As Long, ByVal Arg2 As Long)
'' look up "GetChartElement" in help
MsgBox "ElementID " & ElementID & " Arg1 " _
& Arg1 & " Arg2 " & Arg2
End Sub

In a normal module

Public clCht As Class1 'at top of module

Sub SetUpCht()
Set clCht = New Class1
Set clCht.cht = ActiveSheet.ChartObjects(1).Chart
End Sub

Sub CleanUp()
Set clCht = Nothing
End Sub

With a ChartObject on the active sheet, run SetUpCht and click on the chart.
Best to clear public object ref's and terminate the Class when done, see
CleanUp().

You can use the object ref clCht.cht throughout your project, type an
additional dot to get the intellisense. Within the class module simply
"cht".

But you say you want to trap an event when a chart resizes. Excel does not
expose such an event as you can see from the drop down list. Chart items can
resize when series values change. It would be a lot of work but maybe you
could trap the chart Calculate event.. Compare internal dimensions before
and after.

Resizing internal chart items is not straightforward. As it happens recently
"Janwillem van Dijk" invited people to try his resize method. Search this ng
for his name on 03-Aug-05 and ask nicely!

Regards,
Peter T

"Heath" wrote in message
...

Thanks for your reply, Peter.

I haven't used VBA for years (since changing to VB6 and then to .NET). I
remember some of it, but in any case, I have figured out a workaround

after a
few hours of playing about and sifting through MSDN.

I still have a further question you (or others) might be able to help me
solve, though. The help articles about handling events from embedded

charts
suggest creatinga new class module which contains the chart object as a
public object, and then linking the embedded chart to an instance of that
class from within any code module. In summary, make s class module named
myClassMod containing the declaration

Public WithEvents myChartObj as Chart

then from within any code module, use

Sub CreateChartWithEvents()
Dim ChartObjMod as New myClassMod
Set ChartObjMod.myChartObj = Charts.Add
End Sub

which executes fine and correctly creates a new chart and assigns it to

the
chart object in the class object. However, Microsoft suggests that you

can
then add event handling code to the class module, such as

Sub myChartObj_Resize( ... plus argument list... )
... some code here ...
End sub

that should handle events fired by the embedded chart. Well, I've tried,
and it doesn't. The events do not appear to trigger the code. Any clues?

If you can't think of anything, I'll post this as a new question to the

group.

Grateful for your help,
Heath.