Saving DocX to MemoryStream

Mar 3, 2010 at 8:54 AM
Edited Mar 3, 2010 at 8:55 AM


I would like to write DocX document into Memory instead of file. How do I do it?

byte[] storage = new byte[500000];
using (MemoryStream stream = new MemoryStream(storage))

DocX documentWord = DocX.Create(stream);


and when i upload Stream to DB it's empty.

What am I doing wrong?


With regards,



Mar 3, 2010 at 9:38 AM

the following code uses Streams to create and save a file to disk. Try
replacing just the lines
using (FileStream fs = new FileStream(@"Test.docx", FileMode.Create))
// Save the document
with equivalent code for saving into a DB.

// Create a document using a MemoryStream
using (MemoryStream ms = new MemoryStream())
// Create the document
DocX doc = DocX.Create(ms);
doc.InsertParagraph("Hello World", false);

// Replace this with equivalent code for saving a stream to a DB.
using (FileStream fs = new FileStream(@"Test.docx", FileMode.Create))
// Save the document

Please let me know if this helps.
Mar 4, 2010 at 9:22 AM
Edited Mar 4, 2010 at 9:45 AM

Thanks, I already managed to use it that way before you posted :)

I'm having some trouble merging couple of documents from db. DO you happen to know what could be wrong with this?

I am having this exception:


System.InvalidOperationException was unhandled
  Message="Określona część nie istnieje w pakiecie."
       w System.IO.Packaging.Package.GetPart(Uri partUri)
       w Novacode.DocX.InsertDocument(DocX document)
       w Activity_Management.Documents.documentMerge(DocX documentToMergeIn, DocX documentMerged) w C:\Projects\Project.C.Sharp\Activity Management\Activity Management\Documents.cs:wiersz 509
       w Activity_Management.Documents.createBigReport() w C:\Projects\Project.C.Sharp\Activity Management\Activity Management\Documents.cs:wiersz 527
       w Activity_Management.MainFormDzp.mainFormLoad(Object sender, EventArgs e) w C:\Projects\Project.C.Sharp\Activity Management\Activity Management\MainFormDzp.cs:wiersz 165
       w System.Windows.Forms.Form.OnLoad(EventArgs e)
       w System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
       w System.Windows.Forms.Control.CreateControl()
       w System.Windows.Forms.Control.WmShowWindow(Message& m)
       w System.Windows.Forms.Control.WndProc(Message& m)
       w System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       w System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       w System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, Int32 wParam, Int32 lParam)
       w System.Windows.Forms.Form.SetVisibleCore(Boolean value)
       w System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       w System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       w System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
       w Activity_Management.Program.Main() w C:\Projects\Project.C.Sharp\Activity Management\Activity Management\Program.cs:wiersz 704

public static DocX documentCreate(string varPath) {
            DocX docx = DocX.Create(varPath); return docx;
        public static DocX documentLoad(string varPath) {
            DocX docx = DocX.Load(varPath); return docx;
        public static DocX documentLoad(MemoryStream memoryStream) {
            DocX docx = DocX.Load(memoryStream); return docx;

        public static DocX documentMerge(DocX documentToMergeIn, DocX documentMerged) {
            return documentMerged;
        public static void documentSave(DocX documentMerged) {
        public static void createBigReport() {
            DocX documentReport = Documents.documentCreate("C:\\test.docx");
            MemoryStream memoryStream = RaportyDzpKlient.databaseFileRead("1");
            MemoryStream memoryStream1 = RaportyDzpKlient.databaseFileRead("2");
            MemoryStream memoryStream2 = RaportyDzpKlient.databaseFileRead("3");

           DocX do1 = documentLoad(memoryStream);
           DocX do2 = documentLoad(memoryStream1);
           DocX do3 = documentLoad(memoryStream2);

            documentReport = documentMerge(do1, documentReport);
            documentReport = documentMerge(do2, documentReport);
            documentReport = documentMerge(do3, documentReport);



Exception occurs in documentMerged.InsertDocument(documentToMergeIn);

When i modify documentLoad to save file from stream to disk i can read created docX so the stream has required data.

public static DocX documentLoad(MemoryStream memoryStream) {
            DocX docx = DocX.Load(memoryStream);
            return docx;


Mar 9, 2010 at 8:05 PM
Edited Mar 9, 2010 at 8:05 PM

CoffeyCathal I know you're a busy man but can you confirm if there's a problem with Docx.Merge (as i seen other thread having similar problem) or am I doing something wrong or even jump into Word COM just to get this to work. If it's problem with my code i'll try to get some help on stack overflow, but if there's problem inside library there's no sense.

Mar 9, 2010 at 10:01 PM

DocX.Merge was not exhaustively tested. It is likely that a complex
document, a document that contains objects such as (references,
charts, custom styles, citations, figures, a table of contents, custom
lists, custom headers\footers, text boxes, videos or wordart) would
not merge correctly. If document a is merged with document b and
document a is missing one reference then Word2007 will refuse to open
that document.

It is my intention to fully test all of DocX's functionality in May. I
will do this before adding any new features, all open bugs will be
resolved. I know that this is of little help to you right now but I
cannot work on DocX during my final year of study. I can assure you
that DocX is on my mind and I will give it the development time that
it deserves in May.

kind regards and happy coding,