Convert Word document to base64 string C#

.net-4.0, c#, ms-word, vsto

Solution

You are correct - Word has a lock on the current document. In order to get the current document bytes you would need to copy the existing file (`File.Copy`) or save to a new file (`Document.SaveAs` or `IPersistFile.Save`) for reading its content.

Problem

I'm trying to get the base64 representation of an active Word document while it's still open in Word and am getting the following error in ReadAllBytes(): The process cannot access the file 'file path' it is being used by another process ``` public string GetEncodedTemplate() { //Convert a Word document's base64 representation string base64 = String.Empty; _application.ActiveDocument.Save(); string docPath = _application.ActiveDocument.FullName; byte[] binarydata = File.ReadAllBytes(docPath); base64 = System.Convert.ToBase64String(binarydata, 0, binarydata.Length); return base64; } ``` I do understand that the error occurs because the specified document is still open in Word, my question is - Is it still somehow possible to get the base64 representation of the document without resorting to saving to a temp file? I'm using C# .NET 4.0 And MS Office 2010

Original source