Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Process.Start("Excel.exe") exits immediately [duplicate]

I inherited some code that is now not working. It is supposed to use the Process class to start "Excel.exe". But if I break on the next line I can see the my process instance has the error: 'process.BasePriority' threw an exception of type 'System.InvalidOperationException'. The next line of code throws the following exception:

System.InvalidOperationException: Cannot process request because the process has exited.

Here is the code:

private Microsoft.Office.Interop.Excel.Application StartExcel()
{
    // Maximum number of attempts to look for started Excel Application
    const int maxAttempts = 3;
    // Number of milliseconds to wait between attempts to look for started Excel Application
    const int waitTimeMS = 200;

    Microsoft.Office.Interop.Excel.Application result = null;

    // Start Excel
    var process = Process.Start("Excel.exe");
    //process.WaitForInputIdle();

    // Try to find started Excel Application

    int currentAttempt = 1;

    while ((result == null) && (currentAttempt <= maxAttempts))
    {
        // Wait between attempts 
        if (currentAttempt != 1)
        {
            Thread.Sleep(waitTimeMS);
        }

        // List all running Excel automation objects and find the one with the same process id
        IRunningObjectTable lRunningObjectTable = null;
        IEnumMoniker lMonikerList = null;

        try
        {
            // Query Running Object Table 
            if (GetRunningObjectTable(0, out lRunningObjectTable) == 0 && lRunningObjectTable != null)
            {

                // List Monikers
                lRunningObjectTable.EnumRunning(out lMonikerList);

                // Start Enumeration
                lMonikerList.Reset();

                // Array used for enumerating Monikers
                IMoniker[] lMonikerContainer = new IMoniker[1];

                IntPtr lPointerFetchedMonikers = IntPtr.Zero;

                // foreach Moniker
                while (lMonikerList.Next(1, lMonikerContainer, lPointerFetchedMonikers) == 0)
                {
                    object lComObject;
                    lRunningObjectTable.GetObject(lMonikerContainer[0], out lComObject);

                    // Check the object is an Excel workbook
                    if (lComObject is Microsoft.Office.Interop.Excel.Workbook)
                    {
                        Microsoft.Office.Interop.Excel.Workbook lExcelWorkbook = (Microsoft.Office.Interop.Excel.Workbook)lComObject;

                        // Get the Process ID for the Window Handle 
                        uint processId;
                        GetWindowThreadProcessId(new IntPtr(lExcelWorkbook.Application.Hwnd), out processId);

                        if (processId == process.Id)
                        {
                            // Correct automation object found, return Application
                            result = lExcelWorkbook.Application;
                            break;
                        }
                    }
                }
            }
        }
        finally
        {
            // Release ressources
            if (lRunningObjectTable != null) Marshal.ReleaseComObject(lRunningObjectTable);
            if (lMonikerList != null) Marshal.ReleaseComObject(lMonikerList);
        }

        currentAttempt++;
    }
    return result;
}

At the end result = null. Also an excel workbook does appear on my screen but for some reason the process cannot find the Excel Workbooks process ID. I am guessing this is because the process has excited without closing Excel.exe.

It has been running fine for the past year, and just stopped working a couple of days ago. I do not have any experience with the Process class and am not sure what the issue might be as the errors seem very vague. Based on other SO threads it seems it may be exiting. I can not figure out why the process is immediately Exiting.

Edited for clarification.

like image 848
Mwspencer Avatar asked Oct 21 '25 13:10

Mwspencer


1 Answers

If I were you I would try replacing the entire method with:

private Microsoft.Office.Interop.Excel.Application StartExcel()
{
    return new Microsoft.Office.Interop.Excel.Application();
}

It's possible that interop will use the wrong version of excel if you have multiple versions installed on the same machine I suppose?

I have two versions and my interop is opening an instance of excel 2013 when Process.Start("excel") opens the newer version?

Maybe that's the reason for all the stuff?

like image 169
user1274820 Avatar answered Oct 23 '25 03:10

user1274820