none
Excel COM object cannot be opened with Excel app running RRS feed

  • Question

  • I have a DLL that creates an Excel COM object that opens a workbook. The code works completely fine unless Excel is running, even if it does not have a spreadsheet open. With Excel open the following line runs and runs without returning until Excel is closed:

    var oExcel = new Excel.Application();

    Has anyone else encountered this situation and do they know the work around?

    Thanks,

    Ian

    Thursday, July 7, 2016 8:07 PM

All replies

  • I have a DLL that creates an Excel COM object that opens a workbook. The code works completely fine unless Excel is running, even if it does not have a spreadsheet open. With Excel open the following line runs and runs without returning until Excel is closed:

    var oExcel = new Excel.Application();

    Has anyone else encountered this situation and do they know the work around?

    Thanks,

    Ian

    I think if you are externally trying to launch Excel, then you should first try to attach to a running Excel instance first, should it exist. In other words, I would call GetActiveObject() first, and if that fails, then call new Excel.Application(). In pseudocode, it would look something like:

    try
    {
       oExcel = (Excel.Application) Marshal.GetActiveObject("Excel.Application");
    }
    catch(Exception)
    {
       oExcel = new Excel.Application();
    }


    Thursday, July 7, 2016 10:21 PM
  • Hi bar_bear_ian,
    Thank you for posting here.
    The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.
    Since your issue is about  excel for developer, I will move your thread to that forum for better support. If you have any problem, please feel free to contact us.
    Thanks for your understanding.
    Best Regards,
    Hart

    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.

    Friday, July 8, 2016 5:19 AM
  • Hi bar_bear_ian,

    Have you solved your issue when you followed Brian Muth's suggestion? If not, could you provide more information about this issue, for example sample codes, screenshot and exception etc., that will help us reproduce and resolve it.

    Thanks for your understanding.
    Tuesday, July 26, 2016 9:19 AM