Click here to Skip to main content
15,886,823 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Dear All,

I have a WinForm application (C#) Where we evaluate the excel worksheet done by the user.

If the user forgot to save the excel sheet then we save all running excel sheets and if the user leaves the cursor in the cell and hit submit then it's not saving the file.

Can anyone please help me?


Thanks

What I have tried:

C#
public static bool SaveExcelFile(IQuestion question, string filename, string sheetName)
        {
            ExcelInterop.Application excelApplication = null;
            try
            {
                /* Ref: https://stackoverflow.com/questions/7916711/get-the-current-workbook-object-in-c-sharp */
                excelApplication = (ExcelInterop.Application)Marshal.GetActiveObject("Excel.Application");
                excelApplication.Visible = true;
                excelApplication.DisplayAlerts = false;
                excelApplication.ScreenUpdating = false;

                ExcelInterop.Workbook excelWorkbook = excelApplication.ActiveWorkbook;
                ExcelInterop.Worksheet excelWorksheet = excelWorkbook.Sheets[sheetName];

                if (excelWorksheet != null)
                {
                    excelWorkbook.Close(true, filename);
                }

                return true;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                QuitApplication(excelApplication);
            }
        }
Posted
Comments
Richard MacCutchan 22-Nov-20 6:00am    
Where in your code is the problem?
Richard Deeming 24-Nov-20 0:52am    
You are throwing away the exception details, so you have no idea what the problem is.

Change your catch block to log the full details of the exception. Then start investigating that exception.

Without the exception details, nobody can help you.
abdul subhan mohammed 10-Dec-20 2:56am    
I don't have any errors. But, if the user is leaving the cursor as blinking in the cell, it's unable to save the existing excel worksheet.

The 'excelApplication' is null.

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