URGENT: Unable to open a word template (embedded resource) from MemoryStream


#1

Hi,

I have a problem opening a word document (embedded resource) from a memory stream. I always get the error message: Cannot access a closed stream. But I’m sure the stream isn’t closed. I’m using v. 2.1.12.0.

Heres a sample code:

Stream wordStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(typeof(Class1), “PressClippingTemplate.doc”);
byte[] buffer = new byte[wordStream.Length];
MemoryStream memStream = new MemoryStream(buffer.Length);
wordStream.Read(buffer, 0, buffer.Length);
memStream.Write(buffer, 0, buffer.Length);
// wordStream.Close();
//memStream.Close();

Word.License license = new Word.License();
license.SetLicense(Assembly.GetExecutingAssembly().GetManifestResourceStream(typeof(Class1), “Aspose.Custom.lic”));

Word.Document document = null;
document = new Word.Document(memStream);

Any hints? It’s urgent because this is critical to my application…
Thanks in advance

Michael Mann


#2

I guess you need to:

memStream.Position = 0;

before you can read anything from it after you just wrote to it.

Why do you need to bother with the memory stream by the way? You can just pass wordStream to the Document ctor.


#3

Thanks for the fast response. That was something I didn’t think about. I always read to the end of the stream and forgot the sequential access. Without this it works fine.

I thought the handling is the same as with the excel component. With excel sheets my sample code works.

So thanks again.
Michael