Click here to Skip to main content
15,884,603 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my project, I have a subprocess with a function for searching for a user name in all the sheets and displaying them in a listbox. When an item in the list is selected, it displays that month. This works good, but if the user closes the workbook and selects another entry, I check for the file to be opened and if not I reopen it that is when I get the error.

"System.Runtime.InteropServices.COMException: 'The RPC server is unavailable."

C#
try
            {
                //Check and see if the user closed the workbook
                frmFoodPantry.objApp.Workbooks.get_Item(RegGlobals.excelFileName);
            }
            // If the work book was not open then open the workbook, then open the requeste form.
            catch
            {
                // The user did close the workbook so reopen it
                frmFoodPantry.theWorkbook = frmFoodPantry.objApp.Workbooks.Open(RegGlobals.StrPath);

            }

The line that errors is in the catch pharse.

What I have tried:

C#
try
            {
                //Check and see if the user closed the workbook
                frmFoodPantry.objApp.Workbooks.get_Item(RegGlobals.excelFileName);
            }
            // If the work book was not open then open the workbook, then open the requeste form.
            catch
            {
                // The user did close the workbook so reopen it
                frmFoodPantry.theWorkbook = frmFoodPantry.objApp.Workbooks.Open(RegGlobals.StrPath);
            }
Posted
Updated 14-Aug-22 3:40am
v2
Comments
0x01AA 14-Aug-22 8:56am    
a.) I assume RegGlobals.excelFileName holds the whole path and filename of the excel file.
But for the check with frmFoodPantry.objApp.Workbooks.get_Item(...) you should pass _only_ the filename without path.

b.) "System.Runtime.InteropServices.COMException: 'The RPC server is unavailable"
That sounds like excel has stopped running. Either by crash or killed in taskmanager.
Usually when user closes the excel you opened by interop, the your instanciated excel.exe keeps running.
PaulaJoannAllen 14-Aug-22 11:20am    
excelFileName = "Food Pantry Registry.xlsx";

The trouble is when the "Food Pantry Registry.xlsx" is closed by the user, so it needs to be reopened.
0x01AA 14-Aug-22 11:30am    
But that means if the user closes the excel it does also stop the excel which you instanciated by interop... check in task manager wheter this really happens. If I do that the same, the excel remains running even I close it by myself.

Do you set some special properties when you instanciate the excel in c#?
0x01AA 14-Aug-22 11:48am    
[deleted]
PaulaJoannAllen 14-Aug-22 11:47am    
Yes, excel is still running.
No, I don't do any special properties.

1 solution

You are using a general catch in your code so you have no idea why the exception was thrown. You need to look at exactly why it happened and use the appropriate recovery code. So use the debugger to see exactly what is happening at that point: What are the details of the exception, what are the values of the objects you are using to try and open the workbook, etc.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900