none
It is slow that Split Word document

    Question

  • Hi.

    I use C# develop  a program.

    I open a word document and split the document to 160+ little word documents.  I spent 100s+ on save little files.  

    It is slow. 

    I need to split a document(4.8M) and save 160+ documents in 10s. What do I do ? 

                Word.Document docto = CreateDocument(null);
                Word.Range tocopy = null;
                object oStart = null, oEnd = null;
                object missing = System.Reflection.Missing.Value;
                object saveFormat = Word.WdSaveFormat.wdFormatDocumentDefault;
                object filename = null;
                foreach (SectionUnit unit in list)
                {
                    oStart = unit.Start;
                    oEnd = unit.End;
                    filename = String.Format("{0}\\{1}.docx", path, unit.ID);
                    //Console.WriteLine(String.Format("{0} : {1},{2}", filename, unit.Start, unit.End));
                    CopyPerformance.GetStart();
                    if ((int)oStart >= (int)oEnd)
                    {
                        docto.Content.Delete(ref missing,ref missing);
                    }
                    else
                    {
                        tocopy = _Doc.Range(ref oStart, ref oEnd);
                        tocopy.Copy();
                        docto.Content.Paste();
                    }
                    CopyPerformance.CountTime();
                    
                    SavePerformance.GetStart();
                    docto.SaveAs(ref filename, ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing, ref missing);
                    SavePerformance.CountTime();
                }
                docto.Close(ref missing, ref missing, ref missing);



    Roy Wang


    • Edited by Roy.Wang Friday, July 05, 2013 3:19 AM
    Friday, July 05, 2013 2:18 AM

Answers

  • Hi Roy

    You don't really provide enough Word API code for us to follow how the task is being performed. CreateDocument, SectionUnit, CopyPerformance, etc. aren't from the Word API; we have no idea what kinds of actions these methods perform.

    You also don't tell us the version or file formats you need to work with.

    Without this information, it's not possible to give you much in the way of detailed help. When working with the Word object model, 100s for copying content into 160 new documents and saving them isn't bad performance! The Word application requires that time.

    My suggestion would be to not use the Word APIs at all. Instead, work directly with the Word Open XML file, from disk. You can find more information at OpenXMLDeveloper.org.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, July 05, 2013 3:18 PM

All replies

  • Hi ,

    How to save word document into memory stream?

    I split word document to many little word documents.

    if direct save little documents into disk file, it is slow.



    Roy Wang

    Friday, July 05, 2013 6:14 AM
  • Hi Roy

    You don't really provide enough Word API code for us to follow how the task is being performed. CreateDocument, SectionUnit, CopyPerformance, etc. aren't from the Word API; we have no idea what kinds of actions these methods perform.

    You also don't tell us the version or file formats you need to work with.

    Without this information, it's not possible to give you much in the way of detailed help. When working with the Word object model, 100s for copying content into 160 new documents and saving them isn't bad performance! The Word application requires that time.

    My suggestion would be to not use the Word APIs at all. Instead, work directly with the Word Open XML file, from disk. You can find more information at OpenXMLDeveloper.org.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, July 05, 2013 3:18 PM