Search code examples
exceldelphims-officeoledelphi-2007

Keeping Excel open after Application ends (Delphi Automation)


I'm using Delphi 2007's ExcelXP components as explained here to connect to MS Excel and populate some fields in a worksheet. That works as it should, I can modify the worksheet all I want.

However, I want Excel to stay open after the application ends, but only if there is an open worksheet in Excel. I can get the number of opened workbooks with ExcelApplication.Workbooks.Count, so it would be an easy task to check whether any workbooks are opened when the application ends, and to close Excel during the OnDestroy or similar.

Right now I am stuck though in a situation where Excel closes regardless if there are any opened workbooks as soon as my application ends. Not due to any .Disconnector .Quit calls, it still does that when I take them all out; Excel still closes as soon as my application closes.

Does anybody have any idea as to how I can get Excel to stay open if there are open workbooks, and close it when there are none?

Update: The AutoQuit property of my TExcelApplication was set to True. Turns out that was the problem all along.


Solution

  • Answering my own question, my coworker noticed that the TExcelApplication component has an AutoQuit property. Set that to False and Excel stays open. Now I can manually quit/disconnect Excel in my OnDestroy events.