I am using FileSystemWatcher to monitor an excel file on the network and to display any changes on screen.
However, because the end user has the excel file open, everytime they save it the following code generates an error:
Dim
workbook As Infragistics.Documents.Excel.Workbook = Infragistics.Documents.Excel.Workbook.Load(ResultsFile)
worksheet = workbook.Worksheets(MeetingDate)
Error generated: The process cannot access the file 'C:\Users\Chris\Documents\2012\2012 Results.xls' because it is being used by another process.
My question is, how can I open the excel document read only?
Thanks
Hello,
Instead of passing the name of the file as a String, use the overload for Load() that takes a Stream. This way you can control how the file is opened.
http://msdn.microsoft.com/en-us/library/tyhc0kft.aspx
Hi
Thanks for the quick response but even using filestream still throws the same error
FS =
New FileStream(ResultsFile, FileMode.Open, FileAccess.Read, FileShare.Read)
workbook = Infragistics.Documents.Excel.
Workbook.Load(FS)
The other user opens the excel file, makes a change and clicks save. The FileWatcher triggers the event and trying to load the excel data says the file is still in use.
Is there a way around this or will I need to try a different approach using your Excel control?
I believe your question is answered in this thread: http://community.infragistics.com/forums/p/67599/342147.aspx#342147
If not, please let me know.
Brilliant. It was just the FileShare option being set to Read and not ReadWrite.
All working now, much appreciated.