0

Here is a cut-down version of what I am trying to do. I want to open an excel file into an Excel application, then alter the data in one of the sheets and resave the file. When I try to save, I get the error "The process cannot access the file because it is being used by another process".

The problem is that the file is still open after the Workbooks.Open. Is there any way that I can get the Excel process to release the file? I can make it release by doing a Marshal release on the Excel objects, but then I lose the data. I could store it external to the objects and re-add it, but I'd rather find a way to simply de-couple the file from the Excel objects in such a way that the file is closed. Is there a way to do that?

Microsoft.Office.Interop.Excel.Workbook workBook2;
Microsoft.Office.Interop.Excel.Application _application;
public void ReadAndSave(string path)
{
    bool isLocked1 = IsFileStillInUse(path); // this returns false

    _workBook = _application.Workbooks.Open(path);

    bool isLocked2 = IsFileStillInUse(path);  // this returns true

    if (isLocked2)
    {
        // How can I get the file to release so I can resave it?
    }
}
public static bool IsFileStillInUse(string filePath)
{
    if (!File.Exists(filePath))
    {
        return false;
    }

    try
    {
        using (StreamReader reader = new StreamReader(File.Open(filePath, FileMode.Open, FileAccess.ReadWrite)))
        {
            return false;
        }
    }
    catch (IOException)
    {
        return true;
    }
}
Daniel
  • 9,491
  • 12
  • 50
  • 66
  • This may be relevant:http://stackoverflow.com/questions/158706/how-to-properly-clean-up-excel-interop-objects. As an advice, avoid using COM or MS-Office Interop with C#, use a tool that does not require MS-Office. There are several such products. – NoChance Mar 12 '16 at 01:15
  • OpenOffice.org being one of the more widely supported, AFIAK – jleach Mar 12 '16 at 01:48
  • If the file is open in Excel it will be locked. Period. There's no way to get around this and releasing the COM objects won't change the file lock of a workbook open in the Excel application. But I don't understand what you mean by not being able to save? Or editing the file being a problem? You should be able to use the interop for this, otherwise why open it in Excel in the first place? – Cindy Meister Mar 12 '16 at 22:01
  • Thanks, @CindyMeister, your comment got me thinking and I realize that the IsFileStillInUse method (which I swiped from a googled example of how to open and close Excel files) is meant to test files which are being used by *other* processes, to be used just before the file is loaded. I was calling it again before I saved, not realizing that I was creating my own problem. A little more digging showed me that the error thrown from the save wasn't because the file needed to be closed. If you want to put your comment into an answer I'll accept it. – Francine DeGrood Taylor Mar 14 '16 at 23:02
  • I think either your explanation should be the answer OR flagging this question as "off-topic" with the reason "not reproducible" would be the more correct options :-) I wasn't really understanding what was going on -hence all the questions! – Cindy Meister Mar 15 '16 at 06:08

0 Answers0