How to avoid pop up while close the Excel file

BeUnique 2,332 Reputation points
2021-02-22T07:05:35.077+00:00

I am trying to unprotect and protect excel sheet.

i am able to unprotect sheet. but, finally after all processing done, protect sheet not working.

before closing workbook1.Close(); ==> It will throw popup to save changes. ==> How to avoid this

70527-image.png ==> want avoid to popup when close.....????

what could be the problem. below is the code.

string XLpath = "C:\Documents\excel1.xlsx";
static Excel.Application application1 = null;
static Excel.Workbook workbook1 = null;
SaveToXL(dt, XLpworkbookpwd);
CalcFile();
public void SaveToXL(System.Data.DataTable dt, string XLwbkpwd)
{
xlApp = new Excel.Application();
XLWBook1 = xlApp.Workbooks.Open(XLpath);
foreach (Excel.Worksheet wsht_ex in XLWBook1.Worksheets)
{
//unprotect excel sheet with password before saving
wsht_ex.Unprotect(XLwbkpwd);
}
XLWBook1.Save();
XLWBook1.Close();
xlApp.Quit(); // Quit Excel Application
}

public void CalcFile(string sheetName, string XLwbkpwd)  
     {  
  
         try  
         {  
             WriteToXL();  
         }  
         catch (Exception ex)  
         {  
  
         }  
         finally  
         {  
             workbook1.Save(); //data saved after calculation   
             workbook1.Worksheets[sheetName].Protect(XLwbkpwd);  **// Sheet protecting here**  
             **//throw popup to save your changes - Need to Avoid popup to save**  
            ![70503-image.png][2]  
             workbook1.Close(); **// code hanging here.**  
             application1.Quit();  
         }  
     }  
Microsoft 365 and Office | Excel | For business | Windows
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

Answer accepted by question author
  1. Viorel 125.7K Reputation points
    2021-02-22T08:13:32.377+00:00

    Maybe execute workbook1.Save() after Protect (before Close) too.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.