Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
125
Workbook.Load while XLS file is open in Excel
posted

Hi,

 

I'm using Workbook.Load(fileName) to load and excel file. But every time I'm running the program while also having the same file open in Excel 2007, Workbook.Load throws an IOException:

"The process cannot access the file '<path>/<filename>.xls' because it is being used by another process."

Is there any way to open the excel file for reading, while also having it open in another window in excel?

 

Thanks,

Richard

  • 125
    Verified Answer
    posted

    After some search I've found a workaround for this problem:
    Create a FileStream object with the file name, and  FileMode.Open, FileAccess.Read, FileShare.ReadWrite as parameters.

    Looks like Excel is keeping a write lock on the XLS file while it's running and has the file open, so we need to open the file in ReadWrite fileshare mode.

    You can pass in this FileStream object to the Workbook.Load method as parameter, instead of the file name, and the program will then be able to read the file while it's also open in Excel.