none
Excel 2010 System.OutOfMemoryException: The specified dimension is not valid for the current chart type.

    Question

  • I am working on an “Excel 2010 Add-In” in .net (c#) where I need Excel Charts and Named Ranges to have unique names. This will enable me to look them up from other applications given a file path and the unique name. I have previously made it work in Office 2007 and is now trying to include Office 2010 in the application resource sharing. This works fine for Named Ranges but I get an Exception when accessing Chart in VSTO. I have searched this forum with the exception message (see below) but came up with no hits.

    Internet search: Reveals that this exception often occurs when changing from an older version of Office to a new. I have started from scratch, so…
    It seems that this error is mostly common in cases where people try to access the Chart type.
    I expect that it has something to do with the version of VSTO but I don’t really know.

    Working platform Specifics:

    ·         Windows Server 2008 R2 Standard (64bit)

    ·         Fresh install of Office 2010 (64bit)

    ·         Fresh install of Visual Studio 2010 (64bit)

    ·         Reference to: Microsoft.Office.Interop.Excel (Version 14.0.0.0; Runtime Version v2.0.50727)

    Issue:

    System.OutOfMemoryException Message: The specified dimension is not valid for the current chart type.

    Stacktrace last entry: Microsoft.Office.Interop.Excel._Chart.set_Name(String RHS)

    Code snippet from my ‘Excel 2010 Add-In’:

    Fails with above exception:

    using Excel = Microsoft.Office.Interop.Excel

     

    Excel.Application application = Globals.ThisAddIn.Application;

    Excel.Workbook workbook = application.ActiveWorkbook;

    Excel.Chart chart = application.ActiveChart;

    string name = "SomeName";

    activeChart.Name = name; //This line throws exception

     

    Works fine:

    Excel.Application application = Globals.ThisAddIn.Application;

    Excel.Workbook workbook = application.ActiveWorkbook;

    Excel.Range range = application.ActiveCell;

    string name = "SomeName";

    workbook.Names.Add(name, activeCells);

     

    After this the workbook is saved to a shared folder and file path and unique name is saved in a database. After this WPF clients use the resource for graphical input. It works for the Named Ranges, but…

     

    Thanks for your time!

    Thomas D.

     

     

    Monday, February 21, 2011 4:35 PM

All replies

  • Hi Thomas,

    Thanks for posting in the MSDN Forum.

    Would you please clarify where your snippet deploy? Dose it write in the ThisAddIn_Startup?

    Have a great day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, February 23, 2011 1:00 AM
  • Hi Tom,

    Thank you for the reply.

    As to your question:

    Tom: "Would you please clarify where your snippet deploy? Dose it write in the ThisAddIn_Startup?"

    In the mentioned AddIn I have added a new Group with a single Button. In the click event of the Button all of my code is executed. The code is situated in a .NET Class Library project. Actually it is probably best to show you:

    The click event of the button is handled in the code behind of the ribbon customization:

    private void _btnExportLinkClicked(object sender, RibbonControlEventArgs e)

    {

        try

        {

            Thread.CurrentThread.SetApartmentState(ApartmentState.STA);

            Excel.Application application = Globals.ThisAddIn.Application;

            Excel.Workbook workbook = application.ActiveWorkbook;

            Excel.Range activeCellRange = null;

            Excel.Chart activeChart = null;

            if (workbook != null)

            {

                Excel.Window window = application.ActiveWindow;

                if (window != null)

                {

                    activeCellRange = window.RangeSelection;

                }

                activeChart = workbook.ActiveChart;

                Excel2010AddInLinkManager.AddResourceLinkToClipboard(workbook, activeChart, activeCellRange);

            }

        }

        catch (Exception exception)

        {

            MessageBox.Show(exception.Message, Constants.ERROR_OCCURED);

        }

    }

     

    Excel2010AddInLinkManager.AddResourceLinkToClipboard: Is located in the .NET Class Library

    public static void AddResourceLinkToClipboard(Excel.Workbook workbook, Excel.Chart activeChart, Excel.Range activeCells)

    {

        OfficeLinkDTO excelLink = new OfficeLinkDTO()

        {

            ResourceID = Guid.NewGuid(),

            Program = ProgramEnum.Excel,

            Version = VersionEnum.Office2010

        };

     

        if (TryAddingExcelLinkToActiveResource(workbook, activeChart, activeCells, excelLink))

        {

            string filePath = SaveWorkbookAndGetFullFilePath(workbook);

     

            if (filePath != null)

            {

                excelLink.FilePath = filePath;

                SetExcelLinkOnClipboard(excelLink);

            }

        }

        else

        {

            throw new Exception(Constants.NO_SELECTED_SOURCE);

        }

    }

    TryAddingExcelLinkToActiveResource: Is the relevant method and located in the same class as "AddResourceLinkToClipboard"

     

    private static bool TryAddingExcelLinkToActiveResource(Excel.Workbook workbook, Excel.Chart activeChart, Excel.Range activeCells, OfficeLinkDTO excelLink)

    {

        bool resourceFound = false;

        if (activeChart != null)

        {

            Guid existingID = OfficeToMetafileHelperMethods.GetExistingIDFromExcelName(activeChart.Name);

     

            if (existingID == Guid.Empty)

            {

                string name = OfficeToMetafileHelperMethods.GetValidOfficeNameFromGuid(excelLink.ResourceID);

                activeChart.Name = name; //This line throws exception with any input. E.g. name = "Gf9a0f6d36c974243965fca5311277cbe" or name = "AAA"

            }

            else

            {

                excelLink.ResourceID = existingID;

            }

     

            excelLink.ResourceType = ResourceTypeEnum.Chart;

            resourceFound = true;

        }

        else if (activeCells != null)

        {

            string name = OfficeToMetafileHelperMethods.GetValidOfficeNameFromGuid(excelLink.ResourceID);

            workbook.Names.Add(name, activeCells);

            excelLink.ResourceType = ResourceTypeEnum.Table;

            resourceFound = true;

        }

        return resourceFound;

    }

     

    Sorry for posting this amount of code, but I hope it will aid you better than just trying to explain what I am doing.

    I have highlighted (Bold) the line where the Exception occurs in the above method.

     

    Thomas Dalsgaard


     

     
    A great day to you also,
    Wednesday, February 23, 2011 9:54 AM
  • Hello Thomas,

    From what you said, the add-in you are working with was original created as Excel 2007 Add-in. Now it has been upgraded in a machine with Office 2010 and Visual studio 2010 installed. Right? If yes, I think you could do more test on your side to narrow down this issue.

    Please create a very simple Excel 2010 add-in to test the line "chart.Name = name;". Actually, I have also created a VSTO project in a machine with Office 2010 32-bit installed, and this line works fine on my side.

    Also you could create a very simple Excel 2007 add-in. Then upgrade it and see if this issue also happens.

    Hope the information above could help you. If you have any concern on this post, just feel free to follow up. Have a nice day.


    Bessie Zhao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, February 25, 2011 7:15 AM
  • Hello Bessie,

    And thanks for your suggestions. I think you misunderstod me in regards to going from Excel 2007 AddIn to Excel 2010 AddIn. For the exact reason of fearing an erroneous upgrade and because the code is really not that long I made a fresh Excel 2010 AddIn. I ofcause copy pasted some code since the syntax is the same, but no harm can come from that hopefully.

    Your suggestion about upgrading my original code or upgrading a simple AddIn testing the line "chart.Name = name;" is very good. I believe this was blocked from my mind in fear of a faulty upgrade but now it makes perfect sense! If this works it will solve my problem but it will leave me in the dark as to the origin of the problem. I can live with that for now though.

    I will report back the result and thanks again for the fast replies!

    Have a nice day.

    Thomas D

     

    Friday, February 25, 2011 7:54 AM
  • Hello again Thomas.

    Glad to know that I could give you some idea. I will wait for you result. Have a nice day.


    Bessie Zhao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, February 28, 2011 6:37 AM