I need to make to following snippet of code work in the context of an open file. This code needs to run in an Excel add-in to write the currently open document to a database. At line 3, System.IO.File.ReadAllBytes() an error occurs indicating that the document is currently in use. Is there an equivalent method that I can use that will work on an open document? If not, what is the solution?
Foo.DataClasses1DataContext db = new Foo.DataClasses1DataContext();
string ThisDocument = Globals.ThisAddIn.Application.ActiveWorkbook.FullName;
byte[] inputBuffer = System.IO.File.ReadAllBytes(ThisDocument);
Foo.RFP_Document rfpDocument = new Foo.RFP_Document();
rfpDocument.DocumentName = "Some Name";
rfpDocument.DocumentFile = new System.Data.Linq.Binary(inputBuffer);
db.RFP_Documents.InsertOnSubmit(rfpDocument);
db.SubmitChanges();
Here’s a link to a similar question related to VB. How do I copy an open file in VB6?
You could just save a copy of the file using
Workbook.SaveCopyAs, use this one and delete it afterwards.