C# : Chart.SetSourceData -> Exception

HeinrichMREx

New Member
Joined
Dec 9, 2012
Messages
7
Hallo there,

I would appreciate help on two questions :

1) what could be the reason for this line to throw an exception :

Code:
range = worksheet.get_Range("A3", "B4");
myChart.SetSourceData(range.ToString(), PowerPoint.XlRowCol.xlColumns);
Exception Message: Error HRESULT E_FAIL has been returned from a call to a COM component.
I also tried

If I do it this way:

Code:
myChart.SetSourceData(range, Excel.XlRowCol.xlColumns);
I get compile errors:

1> error CS1502: The best overloaded method match for 'Microsoft.Office.Interop.PowerPoint.Chart.SetSourceData(string, object)' has some invalid arguments
1> error CS1503: Argument 1: cannot convert from 'Microsoft.Office.Interop.Excel.Range' to 'string'

2)
) Do you known of some methods, which allow the selection of Excel Datasheets areas, so that it looks like this(with the color lines and dotes on the corner of the selections) ? :

 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
34,349
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
1. The SetSourceData method is expecting the address of the range, not its values.
2. If you select a chart, Excel automatically selects the data cells-that functionality is not directly exposed (at least not in VBA, so I doubt it's part of the PIAs)
 

HeinrichMREx

New Member
Joined
Dec 9, 2012
Messages
7
Thanks for replying.

1. The SetSourceData method is expecting the address of the range, not its values.
I always thought that "A3","D5" the address of the range is, not the values ...

2. If you select a chart, Excel automatically selects the data cells-that functionality is not directly exposed (at least not in VBA, so I doubt it's part of the PIAs)
How can I make sure it is part / not part of the PIA ? Do you have any reference ?

Best,
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
34,349
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
"A3:D5" is the address of the range but that is not what you are passing to setsourcedata. If you use range.tostring you convert its values to strings and if you just pass range you are passing a range object. Note: this is PowerPoint interop code and not Excel.

Check MSDN for the object model, or use the object browser in Visual Studio: Chart.SetSourceData Method (Microsoft.Office.Interop.PowerPoint) for example.
 

Forum statistics

Threads
1,082,099
Messages
5,363,129
Members
400,720
Latest member
Pettel

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top