none
Parameters for COM/Excel Chart.Export in C++ RRS feed

  • Question

  • Hi everyone!

    Please before anyone responds "Just use C# or C++/CLI,"  please bear in mind that .Net managed assemblies are not an option.  The project I am working on requires that .NET *not* be used.  

    I am having some trouble ascertaining the proper parameters for Chart.Export: https://msdn.microsoft.com/en-us/library/Microsoft.Office.Tools.Excel.Chart.Export(v=vs.100).aspx

    I know they are variants, but "Variant" is actually a struct containing several data types.  I am having trouble figuring out what kind of data type is being used for the function.  Every attempt so far as resulted in exceptions.  I normally would deal with this without asking for help, but I am becoming extremely frustrated with it.  The documentation from Microsoft is of no help whatsoever.

    Any suggestions would be greatly appreciated.

    Thank you!  =)

    Tuesday, September 15, 2015 9:05 PM

Answers

  • Hi T.J.

    I am trying to reproduce this issue, however failed. Here is the code sample working for me using C++:

    VARIANT varOpt;
    varOpt.vt = VT_ERROR;
    varOpt.scode = DISP_E_PARAMNOTFOUND;
    
    VARIANT v;
    VariantInit(&v);
    v.vt = VT_BSTR;
    v.bstrVal = SysAllocString(L"gif");
    
    lpChart.Export(_T("C:\\pic1.gif"), v, varOpt);

    In addition, to make the code work, please ensure that the "Editing Language" for is English if you were using English version Excel. You can change the "Editing Language" via Option->Language->Choose Editing Language.

    And if you still have the problem, please share the detail exception you have got and the which version of Excel are you using.

    Hope it is helpful.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, September 17, 2015 7:10 AM
    Moderator
  • Hi T.J.

    I generated the header files for the Excel object via the Class Wizard then choose add class from type library. I am also upload this demo project from here, you can download it and check whether it works for you:

    http://1drv.ms/1NIEdpH

    >>No, I cannot reproduce it in C#. I tried it, and it appears to work properly (with the case that a blank chart produces a 0 byte file - which is a bit odd, in my opinion).<<

    For generate 0 byte file, what's the edit language you were setting in Excel? You can check it via File->Option->Language->Choose Edit Language.

    If it is not English, please try set it to English to see whether it is helpful.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, September 22, 2015 6:18 AM
    Moderator
  • Hi T.J.

    Based on my understanding, when we use MFC application, the template will generate the code to initialize the COM context automatically for us. We can find the AfxOleInit method in the MFCExcelAutomation.cpp, and this method will call OleInitialize function which will call CoInitializeEx for us. 

    You can get more detail about these functions from link below:
    AfxOleInit

    OleInitialize function

    If you still have problem about COM developing with C++, I suggest that you reopen a new thread in Msdn forums - Visual C++ - Microsoft.

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us.

    Thanks for your understanding.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, September 28, 2015 7:47 AM
    Moderator

All replies

  • Hi T.J. Duchene,

    There are three parameters in this method. The first and second of the parameters are string and the third parameter is Boolean.

    You can also refer to the VBA object model and here is example for your reference:
    Chart.Export Method (Excel)

    Code Sample:

    Worksheets("Sheet1").ChartObjects(1) _ 
    .Chart. Export _ 
     FileName:="current_sales.gif", FilterName:="GIF"
    
    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, September 16, 2015 2:59 AM
    Moderator
  • I truly appreciate the thought, but I am not working with .NET but the Microsoft Foundation Classes and the COM interface.   Since the DLL/COM reference specifies the parameters as type VARIANT, the code sample is effectively worthless in solving my problem. 

    I've already tried the parameters as VARIANT/BSTR and it throws a C++ exception, unless I made a mistake somewhere.

    Thanks!

    T.J.


    • Edited by T.J. Duchene Wednesday, September 16, 2015 3:29 AM
    Wednesday, September 16, 2015 3:28 AM
  • Hi T.J.

    I am trying to reproduce this issue, however failed. Here is the code sample working for me using C++:

    VARIANT varOpt;
    varOpt.vt = VT_ERROR;
    varOpt.scode = DISP_E_PARAMNOTFOUND;
    
    VARIANT v;
    VariantInit(&v);
    v.vt = VT_BSTR;
    v.bstrVal = SysAllocString(L"gif");
    
    lpChart.Export(_T("C:\\pic1.gif"), v, varOpt);

    In addition, to make the code work, please ensure that the "Editing Language" for is English if you were using English version Excel. You can change the "Editing Language" via Option->Language->Choose Editing Language.

    And if you still have the problem, please share the detail exception you have got and the which version of Excel are you using.

    Hope it is helpful.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, September 17, 2015 7:10 AM
    Moderator
  • Thank you so much for the above example code, but I am still not having any luck, I am afraid.  I'm using Excel 2010 (x64), the 2010 Interop Assemblies, and VS 2013, Update 4.  The above posted sample you provided did not function either, but rendered the same results.

    Here is a sample of the code in question.

    CChart Chart;
    
    
    VARIANT filter;
    VariantInit(&filter);
    filter.vt = VT_BSTR;
    filter.bstrVal = SysAllocString(L"gif");
    
    Chart.AttachDispatch(pWorksheet.ChartObjects(_variant_t(1)));
    CString test = Chart.get_Name(); 
    
    /* 
    I know I have a valid context, the name returned in the test string is correct.
    */
    
    Chart.Export(L"C:\\TEST.GIF", filter, vtMissing);  
    
    // Export call throws an unhandled exception.
    //vtMissing is from comutil.h 
    // Same result even with _T used on the name parameter.
    SysFreeString(filter.bstrVal);

    It's probably something obvious, like a pointer to the wrong object type, but I can't seem to wrap my mind around it.  This is really my first project using Microsoft COM (usually using UNIX or when on Windows, C#), so any help you can offer is greatly appreciated.  =)  At this point, I am frustrated and behind schedule, so I am willing to entertain all options to find a solution.

    Could it be the fact I am using Excel 2010 (x64) rather than the 32 bit version as the reason for the difference in our results?





    Sunday, September 20, 2015 8:09 PM
  • Hi T.J.,

    Thanks for the detail exception. The code also works well for me if using vtMissing.

    And what's the exception message? Can you reproduce this issue with C#?

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, September 21, 2015 7:42 AM
    Moderator
  • Hi Fei!

    Thanks for taking the time to look at this for me.

    No, I cannot reproduce it in C#. I tried it, and it appears to work properly (with the case that a blank chart produces a 0 byte file - which is a bit odd, in my opinion).

    This is the exeception generated at calling Export from the C++ code: 
    Unhandled exception at 0x76B3C41F in Test.exe: Microsoft C++ exception: COleException at memory location 0x0035F588.

    It's pretty generic, as I expected with COM.  I tracked down the exception with a bit more data.

    - pException 0x00710a50 {m_sc=-2147352573 } COleException *

    + CException {m_bAutoDelete=1 m_bReadyForDelete=0 } CException

    Sorry to say I haven't the experience with COM to identify why a call to Export would trigger this particular exception, especially with the code sample above.  Since it works for you, I am positively at a loss.  The only thing that I can think of at the moment is that the bindings for MFC were not generated properly versus yours.  I do not know if that is an intelligent guess or not. 

    Anything you can tell me might be helpful.

    Thanks so much! =)
    T.J.





    Monday, September 21, 2015 6:37 PM
  • Hi T.J.

    I generated the header files for the Excel object via the Class Wizard then choose add class from type library. I am also upload this demo project from here, you can download it and check whether it works for you:

    http://1drv.ms/1NIEdpH

    >>No, I cannot reproduce it in C#. I tried it, and it appears to work properly (with the case that a blank chart produces a 0 byte file - which is a bit odd, in my opinion).<<

    For generate 0 byte file, what's the edit language you were setting in Excel? You can check it via File->Option->Language->Choose Edit Language.

    If it is not English, please try set it to English to see whether it is helpful.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, September 22, 2015 6:18 AM
    Moderator
  • Hi Fei!

    I apologize for not getting back to you sooner and I appreciate your patience. The project you provided works beautifully.  I even inserted a call to Export into your project and it worked!

    I'm sorry to say that I am still having the same problem.  =(  I've regenerated the necessary headers via the Typelib Library wizard with an entirely new project, but the call to Export still fails.

    Our project is actually a library rather than an application. I've noticed that our code is different in another way that I wanted to ask you about. 

    if (!pExcel.CreateDispatch(TEXT("Excel.Application")))

    CreateDispatch fails for me unless I make a call to CoInitializeEx first, for example:  

    HRESULT result = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);

    So I have pinned down the major differences between our code to be that:

    1. My class is in an MFC library while yours was MFC application.
    2.  For some reason, I need to make additional calls to CoInitializeEx and CoUninitialize to initialize and clean up the COM interface.

    Somewhere along the way, this causes the MFC call to Export to fail, even with the proper parameters. 

    To be perfectly honest, I do not have any idea why that would be necessary to call CoInitializeEx with the MFC headers, only that it is. My knowledge of MFC and COM in general is less than complete, so please feel free to offer any ideas you can think of. 

    Maybe I am missing something obvious when using MFC in a library or calling CreateDispatch?

    Thank you!
    T.J.

    Sunday, September 27, 2015 8:58 PM
  • Hi T.J.

    Based on my understanding, when we use MFC application, the template will generate the code to initialize the COM context automatically for us. We can find the AfxOleInit method in the MFCExcelAutomation.cpp, and this method will call OleInitialize function which will call CoInitializeEx for us. 

    You can get more detail about these functions from link below:
    AfxOleInit

    OleInitialize function

    If you still have problem about COM developing with C++, I suggest that you reopen a new thread in Msdn forums - Visual C++ - Microsoft.

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us.

    Thanks for your understanding.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, September 28, 2015 7:47 AM
    Moderator
  • Thank you for your time, Fei.  

    I understand if you can't help me further, and I will repost elsewhere.  As you are listed as moderator, I would like to reply for the sake of closing our thread in a productive way.

    “Based on my understanding, when we use MFC application, the template will generate the code to initialize the COM context automatically for us.”

    That does not seem to be the case in VS 2013 Update 4 when using the template to create an MFC library. In fact, upon generating the library project, a blank project does not even compile properly unless I make changes to the generated header files.  An example would be removing or commenting out the #import statements from the generated header files.

    I'm not passing judgment, but perhaps in posting this, the team working on Visual Studio might consider looking at the templates.

    “If you still have problem about COM developing with C++, I suggest that you reopen a new thread in Msdn forums - Visual C++ - Microsoft.”

    I will.  =)  

    “The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us.”

    I thought that this was the most appropriate forum: Office Development.  Could you recommend a more appropriate venue, such as one for MFC?  The link you provided leads to a general forum, and it seems likely that a post would be swamped in dozens of others on different subjects.
    Monday, September 28, 2015 9:00 AM
  • Hi T.J.

    >>I thought that this was the most appropriate forum: Office Development.  Could you recommend a more appropriate venue, such as one for MFC?  The link you provided leads to a general forum, and it seems likely that a post would be swamped in dozens of others on different subjects.<<

    Yes, this forum is the correct forum for Office Development. The reason that I suggest that you reopen a new thread in Visual C++ forum is because the remain issue seems more relative to COM and MFC and in that forum there are more communities who have are familiar with COM and Visual C++, so that you will get more effective response.

    That forum mentioned above is Visual C++ forum which used to discuss and ask questions about the Visual C++ IDE, libraries, samples, tools, setup, and Windows programming using MFC and ATL. It is an appropriate forum to discuss the issue for MFC.

    If you have any about Office development issues, please feel free to ask in this forum.

    Hope it is helpful.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, September 29, 2015 5:26 AM
    Moderator