Click here to Skip to main content
15,891,905 members
Home / Discussions / C#
   

C#

 
Questionwhen is Excel shutdown completed? Pin
Harry6667-Aug-09 1:07
Harry6667-Aug-09 1:07 
AnswerRe: when is Excel shutdown completed? Pin
Moreno Airoldi7-Aug-09 2:03
Moreno Airoldi7-Aug-09 2:03 
QuestionRe: when is Excel shutdown completed? Pin
Harry6667-Aug-09 5:06
Harry6667-Aug-09 5:06 
AnswerRe: when is Excel shutdown completed? Pin
Moreno Airoldi7-Aug-09 15:12
Moreno Airoldi7-Aug-09 15:12 
GeneralRe: when is Excel shutdown completed? Pin
Harry66610-Aug-09 23:29
Harry66610-Aug-09 23:29 
GeneralRe: when is Excel shutdown completed? Pin
Moreno Airoldi11-Aug-09 0:10
Moreno Airoldi11-Aug-09 0:10 
GeneralRe: when is Excel shutdown completed? Pin
Harry66611-Aug-09 3:20
Harry66611-Aug-09 3:20 
GeneralRe: when is Excel shutdown completed? Pin
Moreno Airoldi12-Aug-09 1:24
Moreno Airoldi12-Aug-09 1:24 
Yes you are right! Calling Process.Start() before Application.Quit() won't work properly. I made some tests and I confirm it.

The problem is in how Excel optimizes opening documents. Consider the following example (derived from my previous):

C#
Microsoft.Office.Interop.Excel.Application myApp = null;
Microsoft.Office.Interop.Excel.Workbook myWorkbook = null;
try
{
    try
    {
        myApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
        myApp.Visible = true;
        myApp.AskToUpdateLinks = false;
        myApp.DisplayAlerts = false;
        myWorkbook = myApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
        myWorkbook.SaveAs("D:\\test.xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlXMLSpreadsheet,
            System.Type.Missing, System.Type.Missing, false, false,
            Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, System.Type.Missing,
            System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);

    }
    finally
    {
        if (myWorkbook != null)
        {
            myWorkbook.Close(false, "", null);
            myWorkbook = null;
        }
    }

    System.Threading.Thread.Sleep(3000);
    System.Diagnostics.Process.Start("D:\\test.xls");
    System.Threading.Thread.Sleep(3000);
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
    if (myApp != null)
    {
        myApp.Quit();
        myApp = null;
    }
}


You will clearly see that since one Excel task is still active (the one we instanced for opening and saving the workbook), the call to Process.Start() will cause it to be the container for the re-opening of the document. Since we call Application.Quit() right after that, we close the workbook! This "task optimization" is an Excel internal, so I doubt there's any way to bypass it.

All the other differences between your code and my example don't seem to count: it's all basically the same.

I think you'll just have to call Application.Quit() before opening the file. There's probably no way to avoid that (unless you use another application - not Excel - to show the file...).

If you are working on multiple documents, and you want to open each saved file while processing the next, you may consider opening a different Excel.Application for each single one. There will sure be an overhead, but you can make some tests and see if it's acceptable (it probably is).

This (brutal) example works fine:

C#
for (int i = 1; i < 6; i++)
{
    Microsoft.Office.Interop.Excel.Application myApp = null;
    Microsoft.Office.Interop.Excel.Workbook myWorkbook = null;
    try
    {
        try
        {
            myApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            myApp.Visible = true;
            myApp.AskToUpdateLinks = false;
            myApp.DisplayAlerts = false;
            myWorkbook = myApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
            myWorkbook.SaveAs("D:\\test" + i.ToString() + ".xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlXMLSpreadsheet,
                System.Type.Missing, System.Type.Missing, false, false,
                Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, System.Type.Missing,
                System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);

        }
        finally
        {
            if (myWorkbook != null)
            {
                myWorkbook.Close(false, "", null);
                myWorkbook = null;
            }
            if (myApp != null)
            {
                myApp.Quit();
                myApp = null;
            }
        }

        System.Diagnostics.Process.Start("D:\\test" + i.ToString() + ".xls");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}


Good luck, let me know how it goes! Smile | :)

2+2=5 for very large amounts of 2
(always loved that one hehe!)

GeneralRe: when is Excel shutdown completed? Pin
Harry66612-Aug-09 2:09
Harry66612-Aug-09 2:09 
GeneralRe: when is Excel shutdown completed? Pin
Moreno Airoldi12-Aug-09 2:15
Moreno Airoldi12-Aug-09 2:15 
AnswerRe: when is Excel shutdown completed? Pin
Luc Pattyn7-Aug-09 5:31
sitebuilderLuc Pattyn7-Aug-09 5:31 
GeneralRe: when is Excel shutdown completed? Pin
Harry6668-Aug-09 1:38
Harry6668-Aug-09 1:38 
GeneralRe: when is Excel shutdown completed? Pin
Luc Pattyn8-Aug-09 1:47
sitebuilderLuc Pattyn8-Aug-09 1:47 
GeneralRe: when is Excel shutdown completed? Pin
Harry6668-Aug-09 2:22
Harry6668-Aug-09 2:22 
GeneralRe: when is Excel shutdown completed? Pin
Moreno Airoldi11-Aug-09 0:11
Moreno Airoldi11-Aug-09 0:11 
QuestionActive Crystal Report Pin
sharad Pyakurel7-Aug-09 0:45
sharad Pyakurel7-Aug-09 0:45 
QuestionCommunicating with another form... Pin
Baeltazor7-Aug-09 0:39
Baeltazor7-Aug-09 0:39 
AnswerRe: Communicating with another form... Pin
MarkB7777-Aug-09 0:44
MarkB7777-Aug-09 0:44 
QuestionCreating email reports with attached PDFs Pin
anderslundsgard7-Aug-09 0:35
anderslundsgard7-Aug-09 0:35 
AnswerRe: Creating email reports with attached PDFs Pin
Arindam Sinha7-Aug-09 2:06
Arindam Sinha7-Aug-09 2:06 
QuestionHow to create an application that lists folders containing the most disk space? Pin
Luke Perrin7-Aug-09 0:21
Luke Perrin7-Aug-09 0:21 
AnswerRe: How to create an application that lists folders containing the most disk space? Pin
MarkB7777-Aug-09 0:49
MarkB7777-Aug-09 0:49 
AnswerRe: How to create an application that lists folders containing the most disk space? Pin
Luc Pattyn7-Aug-09 1:11
sitebuilderLuc Pattyn7-Aug-09 1:11 
AnswerRe: How to create an application that lists folders containing the most disk space? Pin
Tamer Oz7-Aug-09 2:03
Tamer Oz7-Aug-09 2:03 
QuestionConvert pcl to PDF Pin
abbd6-Aug-09 23:19
abbd6-Aug-09 23:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.