Can I get a chart object (excel.ChartObject) from OLE_chart.object

C

chris

I have procedures in VB6 for adding charts on excel worksheets. I use
variable EXL_CHRT (As excel.ChartObject) for setting source data etc.

Now I created an OLE object on a form for chart preview before it is
created. I can access it's worksheet with "OLE_chart.object.Worksheets(1)"
for using my existing procedures for adding charts on worksheets.

Can I get a chart object (As excel.ChartObject) from
OLE_chart.object.Charts(1) in order to use existing procedures for setting
chart settings ?

ex.

dim EXL_CHRT As excel.ChartObject

SET EXL_CHRT = OLE_chart.object.Charts(1)
EXL_CHRT.SetSourceData........

Thanks in advance!
 
P

Peter T

I have no idea what you are after but the code snippets are not right at all
dim EXL_CHRT As excel.ChartObject

SET EXL_CHRT = OLE_chart.object.Charts(1)

instead

Set EXL_CHRT = xlSheet.ChartObjects(1) ' or .ChartObjects("some-name")

where xlSheet refers to the sheet containing the chartobject

You may then want to refer to
EXL_CHRT.Object.Some-method-or-property

FYI, "Charts" refers to the collection chart-sheets in the workbook, if any.
A Chart-sheet can contain chartobjects though that's not typical.

If the above does not clarify describe what you have and what you want to
do.

Regards,
Peter T
 
C

chris

Thanks Peter for your message.

My existing procedures use (dim EXL_CHRT As excel.ChartObject) for setting
chart's properties.

Now I created an OLE object (Excel Chart). (Dim EXL_OLE_chrt As Excel.Chart
- Set EXL_OLE_chrt = ole_excel_chart.object.Charts(1)).

I was wondering whether I can access same object from both (EXL_CHRT &
EXL_OLE_chrt) so I can pass them as parameter to procedure for setting chart
properties instead of having 2 procedures, one for each type (ChartObject or
Chart).
 
P

Peter T

Set EXL_OLE_chrt = ole_excel_chart.object.Charts(1)).

You say you created a chart with the above but that cannot work in Excel, as
I tried to explain before that syntax is incorrect.

Assuming you are working with an embedded chart on a sheet

Dim xlChtObj as Excel.Chart
Dim xlCht as Excel.Chart

' assumes an embedded chart exists on the sheet
Set xlChtObj = xlSheet.ChartObjects(1)
Set xlCht = xlChtObj.Object.Chart

call foo(xlChtObj.Object)
or
call foo(xlCht)

Function foo(cht as Excel.Chart)
'do stuff
End Function

Note the object property of the "ChartObject" is the "Chart" (ChartObject
might have been better named as say "ChartHolder").

If you are working with a chart sheet, the entire sheet is a "Chart" object,
you can also have embedded ChartObjects on a Chart sheet.

Regards,
Peter T
 
C

chris

Thanks Peter.

I use the commands below for accessing a chart in OLE object.

Dim exl_chrt As Excel.Chart

Set exl_chrt = ole_excel_chart.object.Charts(1)

My existing procedures had a parameter to CHARTOBJECT instead of CHART.
That's why I was trying to retrieve the CHARTOBJECT from the 2 types of
charts I was creating. I changed parameter to be CHART and retrieve chart
object for both charts and it works.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top