1

I'm working on c# windows application with SQL server. I'm getting an error like the following when tried to do Save-as in excel in c#. But inside workbook.open statement and workbook.saveas am changing readonly attribute as false.I need to save the excel with the same name only.Can anybody please help me.

Cannot access readonly document filename.xlsx

private static Microsoft.Office.Interop.Excel.Workbook mWorkBook;
    private static Microsoft.Office.Interop.Excel.Sheets mWorkSheets;
    private static Microsoft.Office.Interop.Excel.Worksheet mWSheet1;
    private static Microsoft.Office.Interop.Excel.Application oXL;

    string path = @"D:\Test\test.xlsx";
    oXL = new Microsoft.Office.Interop.Excel.Application();
    oXL.Visible = true;
    oXL.DisplayAlerts = false;
    mWorkBook = oXL.Workbooks.Open(path, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
    //Get all the sheets in the workbook
    mWorkSheets = mWorkBook.Worksheets;
    //Get the allready exists sheet
    mWSheet1 = Microsoft.Office.Interop.Excel.Worksheet)mWorkSheets.get_Item("Sheet1");
    mWSheet1.Cells[i+2, 5] = DBDaysWorked;
    mWorkBook.SaveAs(path,                                              System.Reflection.Missing.Value,
    System.Reflection.Missing.Value,
    System.Reflection.Missing.Value,
    false,false,                         Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared,
    false,
    false,
    System.Reflection.Missing.Value,
    System.Reflection.Missing.Value,
    System.Reflection.Missing.Value);
    mWorkBook.Close(Missing.Value, Missing.Value, Missing.Value);
    mWSheet1 = null;
    mWorkBook = null;
    oXL.Quit();
    GC.WaitForPendingFinalizers();
    GC.Collect();      
3
  • I know it's not to do with what you asked, but as a side note don't forget to release your COM objects as soon as you're finished with them (if you're not already). Example: Marshal.ReleaseComObject(mWSheet1);. You usually do this "smallest" first, so Worksheet, Workbook and then Excel. I know it's dirty but I usually then call GC.WaitForPendingFinalizers() and GC.Collect().
    – Equalsk
    Jul 1, 2015 at 13:17
  • I released the com objects ,but still i cant save
    – user1500633
    Jul 2, 2015 at 6:31
  • Your updated code doesn't show this. Before the garbage collection you should have Marshal.ReleaseComObject(mWSheet1), then the same for mWorkBook and if you're not planning on re-using it, oXL. As I stated though this is nothing to do with your issue, it's just good practice when dealing with Office COM objects.
    – Equalsk
    Jul 3, 2015 at 7:08

1 Answer 1

1

The problem is with the flag XlSaveAsAccessMode.xlShared when saving the document, it throws the error:

This workbook cannot be shared because privacy has been enabled for this workbook. To share this workbook, click the File tab, and then click Excel Options. In the Excel Options dialog box, click Trust Center, and then click the Trust Center Settings button. In the Privacy Options category, clear the check box next to the option Remove personal information from file properties on save.

Removing this option does indeed fix the error. Alternatively setting the flag to XlSaveAsAccessMode.xlNoChange works if you're not bothered about sharing the workbook.

I'm not sure if you can automate removing this property from a workbook to prevent the error.

For the record, I found this by simply wrapping your code in a try/catch and throwing an error...

Garbage Collection

Before the try block that you should have now added hint hint ;-)

Initialise everything to null so that it will be accessible in the finally block.

Microsoft.Office.Interop.Excel.Workbook mWorkBook = null;
Microsoft.Office.Interop.Excel.Sheets mWorkSheets = null;
Microsoft.Office.Interop.Excel.Worksheet mWSheet1 = null;
Microsoft.Office.Interop.Excel.Application oXL = null;

You normal code is fine and can go in the try block. Catch only errors you can handle and recover from. Then in the finally block:

if (mWorkBook != null)
{
    mWorkBook.Close(Missing.Value, Missing.Value, Missing.Value);
}

if (oXL != null)
{
    oXL.Quit();

    if (mWSheet1 != null)
    {
        Marshal.FinalReleaseComObject(mWSheet1);
        mWSheet1 = null;
    }

    if (mWorkSheets != null)
    {
        Marshal.FinalReleaseComObject(mWorkSheets);
        mWorkSheets = null;
    }

    if (mWorkBook != null)
    {
        Marshal.FinalReleaseComObject(mWorkBook);
        mWorkBook = null;
    }

    Marshal.FinalReleaseComObject(oXL);
    oXL = null;
}

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
3
  • still its showing same error,cannnot access readonly document .xlsx
    – user1500633
    Jul 4, 2015 at 6:40
  • If i'm using save instead of saveas ,no error will show,but the values not saved.
    – user1500633
    Jul 4, 2015 at 6:53
  • Then the problem is with your spreadsheet, not the code. The only way I can get the same error is by setting the file to read-only, and obviously it works if I switch read-only back off. Just to check, you really have right clicked the Excel file, gone to Properties and unchecked Read-only...
    – Equalsk
    Jul 4, 2015 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.