Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hello friends
I'm trying to automate an excel file within a chart,and I want to export the chart to JPG file or something like this,but I got this error :
Exception from HRESULT: 0x800401A8
I searched hours and tried many codes but every time I get the same error.
here is my code:

C#
Excel._Workbook workBook;
        Excel.Series series;
        Excel.Range range;
        Excel._Chart chart;

        workBook = (Excel._Workbook)workSheet.Parent;
        chart = (Excel._Chart)workBook.Charts.Add(Missing.Value, Missing.Value,
            Missing.Value, Missing.Value);

        range = workSheet.get_Range(string.Format("C1:C{0}", rowCount), Missing.Value).get_Resize(
            Missing.Value, 1); ;
        chart.ChartWizard(range, Excel.XlChartType.xlColumnClustered, Missing.Value,
            Excel.XlRowCol.xlColumns, Missing.Value, Missing.Value, false,
            Missing.Value, Missing.Value, Missing.Value, Missing.Value);
        series = (Excel.Series)chart.SeriesCollection(1);
        series.XValues = workSheet.get_Range("A1", string.Format("B{0}", rowCount));

        series = (Excel.Series)chart.SeriesCollection(1);

        chart.ChartArea.Font.Name = "Tahoma";
        chart.AutoScaling = true;
        chart.ChartStyle = 27;

        Excel.Axis yaxis = (Excel.Axis)chart.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlPrimary);

        yaxis.MinimumScaleIsAuto = false;
        yaxis.MinimumScale = 0;

        yaxis.MaximumScaleIsAuto = false;
        yaxis.MaximumScale = 10;

        chart.HasTitle = true;
        chart.ChartTitle.Text = "title";

        chart.Location(Excel.XlChartLocation.xlLocationAsObject, workSheet.Name);

        range = (Excel.Range)workSheet.Rows.get_Item(3, Missing.Value);
        workSheet.Shapes.Item("Chart 1").Top = (float)(double)range.Top;
        range = (Excel.Range)workSheet.Columns.get_Item(5, Missing.Value);
        workSheet.Shapes.Item("Chart 1").Left = (float)(double)range.Left;

        chart.Export(@"C:\Chart1.jpg", "JPG", false);
Posted
Comments
skydger 22-Dec-12 14:00pm    
How do you initialize your Excel appliacation and workbook?
Have you opened a workbook? Does workSheet exist?
Neema Derakhshan 23-Dec-12 1:01am    
tnx for response sir, yes workbook & worksheet exist
workBook = (Excel._Workbook)(app.Workbooks.Add(Missing.Value));
workSheet = (Excel._Worksheet)workBook.ActiveSheet;
and I import my data in the application..
skydger 23-Dec-12 5:23am    
Have you found in wich line of your code this error occurs?
Neema Derakhshan 24-Dec-12 2:05am    
yes ofcource
chart.Export(@"C:\Chart1.jpg", "JPG", false);

1 solution

I did not had a situation like that but i coded a lot on the interopthing. in a lot of situations, my solution was to programmatically call the vba codemodule via:

Microsoft.Vbe.Interop;

hope this helps...
 
Share this answer
 
v2
Comments
Neema Derakhshan 23-Dec-12 1:12am    
thank you for response, I'm using Microsoft.Office.Interop, I guess there is no difference?is it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900