How to create a Microsoft.Office.Interop.Word.Document object from an array of bytes without saving it to disk?

How can I create a Microsoft.Office.Interop.Word.Document object from an array of bytes without saving it to disk using C #?

 public static int GetCounterOfCharacter(byte[] wordContent) { Application objWord = new Application(); Microsoft.Office.Interop.Word.Document objDoc = new Document(); //objDoc = objWord.Documents.Open(("D:/Test.docx"); i want to create document from byte array "wordContent" return objDoc.Characters.Count; } 
+7
source share
1 answer

There is no direct way to do this, as far as I know. Interactive Word libraries cannot read from a stream of bytes. If you are not working with huge (or huge) files, I would recommend just using the tmp file:

 Application app = new Application(); byte[] wordContent = GetBytesInSomeWay(); var tmpFile = Path.GetTempFileName(); var tmpFileStream = File.OpenWrite(tmpFile); tmpFileStream.Write(wordContent, 0, wordContent.Length); tmpFileStream.Close(); app.Documents.Open(tmpFile); 

I know that this is not the answer you are looking for, but in that case (where it takes quite a lot of time and fijing to do what you really want to do), it might be worth considering that time outweighs the performance at runtime.

If you still want to learn how to solve this problem, as you intend to, I would recommend the answers in this thread.

+14
source

All Articles