Click here to Skip to main content
11,926,429 members (57,904 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# MS-Excel Chart export
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:

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 22-Dec-12 1:57am
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

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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:


hope this helps...
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.151126.1 | Last Updated 22 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100