How to avoid pop up while close the Excel file

Gani_tpt 1,506 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();  
         }  
     }  
C#
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.
10,231 questions
Excel Management
Excel Management
Excel: A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.Management: The act or process of organizing, handling, directing or controlling something.
1,639 questions
{count} votes

Accepted answer
  1. Viorel 112.1K 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