none
Starting Excel using Excel.Application RRS feed

  • Question

  • we have a console application that opens either Excel/Word (if not already open.) 

    The code to open word/excel is as shown below. (The purpose of this program is to open Excel/Word if not already open. So it does not call Quit. It opens Excel/Word and terminates. Our VSTO Addin that loads when Excel/Word is started does some processing and it is user's responsibility to close the Excel/Word.)

    private static void StartWord()
            {
                // WORD IS STILL OPEN AFTER THIS CONSOLE PROGRAM TERMINATES
                Word.Application wd = new Word.Application();
                wd.Visible = true;
                Marshal.FinalReleaseComObject(wd);
                wd = null;
            }
    
            private static void StartExcel1()
            {
                // EXCEL OPENS BUT IS IMMEDIATELY CLOSED AS SOON AS THIS CONSOLE PROGRAM TERMINATES
                // I CAN SEE EXCEL START AND TERMINATE IN TASK MANAGER
                Excel.Application xl = new Excel.Application();
                xl.Visible = true;
                Marshal.FinalReleaseComObject(xl);
                xl = null;
            }
    
            private static void StartExcel2()
            {
                // EXCEL OPENS AND REMAINS OPEN AFTER THIS CONSOLE PROGRAM TERMINATES.
                Excel.Application xl = new Excel.Application();
                xl.Visible = true;
                xl.Workbooks.Add();
                Marshal.FinalReleaseComObject(xl);
                xl = null;
            }
    
            static void Main(string[] args)
            {
                //StartWord();
                StartExcel1(); // NOT WORKING. EXCEL IS CLOSED AS SOON AS THIS PROGRAM TERMINATES
                //StartExcel2();
            }

    The function to start word is StartWord() and it works as it is shown in the above code.

    But the corresponding function to start Excel StartExcel1() does not work. It starts Excel but as soon the console application closes Excel is also terminated.

    If I use StartExcel2() which additionally opens an empty workbook, then Excel remains open and visible even after the console application.  

    My question is is something wrong in the way Excel is started in StartExcel1 function.

    Thank You,

    Rakesh

    Tuesday, December 3, 2013 11:11 AM

Answers

  • Hi rsharma

    Try setting the UserControl property to TRUE before your application finishes.

    Word and Excel work differently in this respect. Word is almost always running with UserControl active; Excel will not if it's started by another program.


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, December 7, 2013 12:02 PM
    Moderator

All replies

  • Hi rsharma,

      xl.Workbooks.Add();
    
    Note, the Workbooks property returns an instance of the Workbooks class from the Excel Object Model which should be released after. Moreover, the Add function returns a new instance of the Woorbook class which should be released too. That is why Excel remains open after the console program terminates. I didn't find anything strange.
    Tuesday, December 3, 2013 2:51 PM
  • Hello Eugene, Thanks for the reply.

    Yes, the instances of  Workbooks and Workbook should also have been released in StartExcel2().

    But the main purpose of the program is keep Excel open and running. Basically my question is about StartExcel1(). Why that function is unable to keep Excel visible and running after Console application terminates.

    (I can use StartExcel2() to open an empty workbook so Excel keeps running, as a workaround, but I wanted to know if there is anything wrong in StartExcel1() which closes Excel immediately.)

    Thank You,

    Rakesh

    Wednesday, December 4, 2013 4:16 AM
  • Hello Rakesh,

    If the host application remains in memory after your app ends, it indicates that you don't release underlying COM objects properly. So, the StartExcel1 method works as expected. In the StartExcel2 method you get an instance of the Workbooks class and a new instance of the Workbook class. They both should be released to let the OS unload Excel from the memory. The Marshal.ReleaseComObject method can be used for releasing unused references.

    Wednesday, December 4, 2013 2:49 PM
  • Hello Eugene,

    I understood that after all instances are released word/excel unloads.

    This is the behaviour I observe in StartExcel1(). But StartWord1() has different behavior. It keeps word running.

    I want to keep Exel running after the console application exits. Is there a way to do that. If I create instances of Excel.Application , Excel.Workbooks etc and do not release them using Marshal.ReleaseComObject() then Excel won't close giving the behavior I want. (Clicking on the Close button in Excel application is closing excel and I do not see excel.exe running in Task manager which is what I want.) But is this correct way of doing it.

    Thank You,

    Rakesh

    Friday, December 6, 2013 6:16 AM
  • Hi Rakesh,

    What is the final goal of running Excel using its Object Model and keeping it running? Could you please explain your goal in details?

    Did you try to use the Process.Start method for starting Office applications instead? Is it what you are looking for?

    Friday, December 6, 2013 4:24 PM
  • Hi rsharma

    Try setting the UserControl property to TRUE before your application finishes.

    Word and Excel work differently in this respect. Word is almost always running with UserControl active; Excel will not if it's started by another program.


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, December 7, 2013 12:02 PM
    Moderator
  • Hello Eugene & Cindy,

    Thank you for the responses.

    I have tried UserControl = true for Excel and it is working and Excel keeps running.

    I can not use Process.Start(), because after Excel/Word Starts I should be able to detect whether or not our addin is loaded which means I will have to use VSTO object model to use the APIs like COMAddIns etc. 

    Other reason is that once the console application (which is responsible for starting word/excel) terminates, our code assumes that our addin which gets loaded as Exce/Word starts, has been initialized and has called some initialization code like connecting to our server etc.). But with Process.Start() I am not able to ensure that this sequence occurs correctly. Because Process.Start seems to return immediately whereas addin initialization code may or may not have completed successfully.

    Thank You,
    Rakesh


    Monday, December 9, 2013 12:02 PM
  • Rakesh,

    Note, you can call a public method of your add-in (before closing the console application) to make sure that your add-in is enabled and initialized correctly. For example, in Word see the AddIns property of the Application class.

    Monday, December 9, 2013 1:14 PM