-1

For reporting I create an excel application, export some data to it, and make it visible for the user.

public void MyMethod()
{
   Excel.Application excelApp = new Excel.Application();
   object misValue = System.Reflection.Missing.Value;
   Excel.Workbook workBook = excelApp.Workbooks.Add(misValue);
   Excel.Worksheet workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(1);
   Excel.Range range;

   //my export code here..



  //at last excel application is shown for user
  excelApp.Visible = true;
}

When the user clicks the "Close" button, a dialog appears that contains ["Save", "Don't save" and "Cancel"]. In any case (save or don't save), the window closes, but the EXCEL.EXE process is live like picture in this question: Like this picture , because I don't close it yet.

How can I define if the user closes the viewed excel window or not? If yes, then I will quit that excel application which I created.

Edit: I need something like "Exit event". To define if user close window, to Quit application.

Community
  • 1
  • 1
Jeyhun Rahimov
  • 3,769
  • 6
  • 47
  • 90

2 Answers2

0

You likely are using a COM object that isn't being released properly

Worksheets sheets = excelApp.Worksheets;
Worksheet sheet = sheets.Open(...);

Marshal.ReleaseComObject(sheets);
Marshal.ReleaseComObject(sheet);
Tsukasa
  • 6,342
  • 16
  • 64
  • 96
  • Make sure you are releasing all COM objects you are creating. Also make sure you are assigning them all to vars so you can release them. – Tsukasa Mar 04 '14 at 13:45
0

Sorry, i misunderstood the question....

Old: Check if the Window exists

[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
...
while(true)
{
    IntPtr hWnd = (IntPtr)FindWindow(windowName, null);
    public static extern bool IsWindow(IntPtr hWnd);
}
App.Close()
serious
  • 525
  • 10
  • 21