Click here to Skip to main content
15,896,500 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
After decrypt file, i have a new problem with save change (/silent) Excel files when close workbook, MSExcel always promts to save and replace file in every xlsx file. i really need to Silent,Quiet save quit excel work book. but, I have no idea what i miss?, Help me please, (Sorry for my Language)
Try2SilentSaveExist.rar - File Upload Script[^]

What I have tried:

C#
Microsoft.Office.Interop.Excel.Application MSexcel = new Microsoft.Office.Interop.Excel.Application();
        Microsoft.Office.Interop.Excel.Workbook excelPrj = null;
        /*Microsoft.Office.Interop.Word.Application MSword = new Microsoft.Office.Interop.Word.Application();
        Microsoft.Office.Interop.PowerPoint.Application MSpoint = new Microsoft.Office.Interop.PowerPoint.Application();
        Microsoft.Office.Interop.Access.Application MSAccs = new Microsoft.Office.Interop.Access.Application();
        Microsoft.Office.Interop.Word.Document wordPrj = null;
        Microsoft.Office.Interop.PowerPoint.Presentation pointPrj = null;*/
        Missing miss = Missing.Value;
        private bool RepairFile(string brokenFile)
        {
            if (System.IO.Path.GetExtension(brokenFile).ToLower() == ".xlsx")
            {
                try
                {
                    string OldFile = brokenFile;
                    MSexcel.DisplayAlerts = false;
                    MSexcel.Visible = false;
                    MSexcel.UserControl = false;
                    excelPrj = MSexcel.Workbooks.Open(OldFile, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, Microsoft.Office.Interop.Excel.XlCorruptLoad.xlRepairFile);
                    if (excelPrj != null)
                    {
                        excelPrj.Close(true);
                    }
                    
                    return true;

                }
                catch { return false; }
            }
            else
            {
                /*
                if (System.IO.Path.GetExtension(brokenFile).ToLower() == ".docx")
                {
                }*/
                return true;
            }
        }
Posted
Updated 1-Oct-16 3:22am
v2

1 solution

You could precede your saving code with this:

C#
if (File.Exists(brokenFile))
{
    File.Delete(brokenFile); 
}
 
Share this answer
 
v2
Comments
Member 12680324 1-Oct-16 13:06pm    
if (excelPrj != null)
                {
                    if (System.IO.File.Exists(brokenFile))
                    {
                        Wtime = System.IO.File.GetLastWriteTime(brokenFile);
                        System.IO.File.Delete(brokenFile);
                    }
                    excelPrj.SaveAs(brokenFile, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, miss, miss,false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,miss, miss, miss, miss, miss);
                    excelPrj.Close(true);
                    System.IO.File.SetLastWriteTime(brokenFile, Wtime);
                }

its WORK Thank you:)

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