none
Can I set color for 'ChartArea' element of "Excel.chart" ole object? RRS feed

  • Question

  • Hi All,

    In general, I am trying to insert "Excel.chart.8" ole object in Word 2007 and change 'ForeColor' of 'ChartArea'. I have partial success :) Please, look at the following code:

    word2007::_ApplicationPtr appl = wordAppl;
    	
    word2007::ShapePtr shape = appl->ActiveDocument->Shapes->AddOLEObject(&_variant_t("excel.chart.8"));    
    
    shape->OLEFormat->Activate();
            
    excel2007::_WorkbookPtr wb = shape->OLEFormat->Object;
    excel2007::_ChartPtr chart = wb->Charts->Item[_variant_t(1)];
    chart->Name = _bstr_t(L"My Chart");    
    chart->Type[0] = 4;    
    
    // set chart area background format
    excel2007::ChartAreaPtr chartArea = chart->ChartArea[0];
    excel2007::ChartFormatPtr chartFormat = chartArea->Format;    
    excel2007::FillFormatPtr fill = chartFormat->Fill;
    
    // I can`t turn on 'Solid' mode
    fill->Solid();
    
    // I can`t set value
    fill->ForeColor->Put__RGB(RGB(255,0,0));
    

    The ole object was succesfully added, and I can change a lot of properties such as chart title, chart type..., I can edit chart data - remove\add series, BUT when I try to do something with FillFormat I receive error. In the sample, when I call 'Solid()' method I receive the following message:

    "Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call.  This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention."

    Frankly speaking, I have no idea how I can fix the situation. Is it Office bug or I do not know something :) ?

    Thanks.


    Wednesday, May 16, 2012 2:05 PM

All replies

  • Hi Michael,

    Have you tried to set up a chart in the excel itself instead of the word app?

    Thanks.

    Friday, May 18, 2012 5:42 AM
  • Hi,

    No, I have not. I can insert "Excel.chart.8" ole object manually in Word: Insert->Object... and change any properties in the object. That`s why I think that everything is ok with the ActiveX.

    I found out one interesting moment: if I deactivate ole object after adding (for example, click at the document area) and after this call my code which activates object and sets ChartArea color, everything will be ok and color of ChartArea will be changed. Unfortunatelly, the behavior of ole object is very unstable, and sometimes I receive the following exception: access denied.

    Friday, May 18, 2012 7:54 AM
  • I found out one interesting moment: if I deactivate ole object after adding (for example, click at the document area) and after this call my code which activates object and sets ChartArea color, everything will be ok and color of ChartArea will be changed. Unfortunatelly, the behavior of ole object is very unstable, and sometimes I receive the following exception: access denied.

    Hi Michael

    I think you're at the point where "if it doesn't work, it doesn't work". This chart type has been superceded in 2007 with the new charting engine, so the old OLE embedded object certainly isn't being supported any more. And yes, the approach via object linking and embedding could be "unstable", especially with Excel when leaving, then attempting to re-enter the link via code. The only stable way I was ever able to find was to force the object to open in a separate Excel window, rather than working in-place.

    Any particular reason you don't want to work with the new charting API? (Note: You need 2007 SP2 in order to have it.)


    Cindy Meister, VSTO/Word MVP

    Monday, May 21, 2012 9:08 AM
    Moderator
  • Yes, in my addin I use new charting API, it works fine, but I don`t like that during calling of 'AddChart' method in Word or PowerPoint the new instance of Excel is started. I can set chart data programmatically and I don`t need visible instance of Excel.  It`s very inconvinient especially when I try to add 15 or 20 charts.

    I would like to use activex as alternative solution, but it works bad.

    In general, I am trying to solve the following task: how to add chart in Word or PowerPoint and hide instance of Excel?

    Monday, May 21, 2012 10:28 AM
  • Hi Michael,

    Regarding “How to add chart in Word or PowerPoint and hide instance of Excel?” - There is no way to achieve this as Excel has to be activated. The workaround will be to set the Excel instance to visible false but you will see a flicker (Excel will be visible and then hide).

    For original issue, I tried using similar C# code but I don’t see any errors. May be there is some C++/environment specific error. Could you try on other machines?

    Thanks,


    Shiv Khare

    Friday, June 8, 2012 12:17 AM
    Moderator