简体   繁体   中英

Problem opening Excel workbook using Excel Interop

I'm writing a C# forms application to open an existing Excel workbook using the Excel Interop. Everything works fine. I can read and write fine. I do not save the workbook until the user close the C# application. The problem happens when my program is running and the user accidentally opens the same workbook with Excel. I get the error:

Exception from HRESULT: 0x800401A8

at this line

Range excelRange = worksheet.UsedRange;

Is there a way to lock the workbook and not allow the user to open it with Excel when my program is running?

Thanks

There's probably some better way of doing this but the only way I can think of would be to:

  1. Copy the Excel file to a temporary location
  2. Lock the file in the original location so noone can access it.
  3. Work with the temporary file.
  4. Save and close the temporary file.
  5. Unlock and delete the original file.
  6. Copy the temporary file to the original location.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM