locked
how to copy cotent of a word document and paste into another word document. RRS feed

  • Question

  • Greetings to all.

    I have a requirement that i want to copy content of a word document and paste its content into the another predefined document with header and footer using C#.NET. checked with the tutorials on Mail Merge but no use. its very helpful if any one gives a solution how it can be done.


    RehaanKhan. M


    • Edited by Ayub Khan M Friday, October 16, 2015 5:47 AM
    Friday, October 16, 2015 4:57 AM

Answers

  • You are aware the Word has an existing system of templates and auto-text?
    You can even make a serial letters from access DB or excel table, wich sounds about what you are trying to re-invent here in C#.
    And it is never a good idea to reinvent the wheel, if you got a fully tested car standing there, keys in the ignition :)

    Regarding: .docx or .doc in C# solution?

    If you need to only support .docx, you can use the OpenXML SDK without any special considerations (regarding where this can run). Or the ZipArchive and XMLWriter clases. That format is the easy case by far.

    If you need to support .doc in addtion, you have to use the (t)rusty COM based Word Interop.
    Wich requries office to be installed, can't run from a service or webserver (no interactive session) and invovles all the pitfalls of working with .COM

    • Proposed as answer by Kristin Xie Friday, October 23, 2015 9:04 AM
    • Marked as answer by Kristin Xie Monday, October 26, 2015 2:01 AM
    Friday, October 16, 2015 11:58 AM

All replies

  • You should specify if you are dealing with .docx or .doc word document.

    Word for Developers is more relevant forum for your question, a moderator will help moving it there, also you can do your search inside that forum for a similar question.


    Fouad Roumieh

    Friday, October 16, 2015 5:34 AM
  • its doc and docx both. and sorry for the bad explanation in Question i want to copy the Complte content of word document into another word Document.

    RehaanKhan. M

    Friday, October 16, 2015 5:45 AM
  • its doc and docx both. and sorry for the bad explanation in Question i want to copy the Complte content of word document into another word Document.

    RehaanKhan. M

    But the second document is it a new one, or it already has content? I'm asking because you need to specify the starting page that you need to start copying to.

    Fouad Roumieh

    Friday, October 16, 2015 5:50 AM
  • Hi Fouad Roumieh,

    Step1: Let me Explain the Complete work flow of application i am developing. Initially i will Create a word Template with Header and Footer and there is no any other content in this document.

    Step2: The Second Document will be having some data Like Paragraphs formatted in particular style, Tables, Images. it will be saved in one folders in my PC Local Folder.

    Step3: Now the All the Contents from the Second Documents has to be Copied to the Word Document template type created in Step1 and it will be saved with some name using saved as method.

    I have gone through mail Merging. But it not fulfills my requirement. I hope you understand what i require.

    Thanks for your Reply.


    RehaanKhan. M




    • Edited by Ayub Khan M Friday, October 16, 2015 6:14 AM
    Friday, October 16, 2015 6:05 AM
  • Hello,

    Myself or another moderator can move your question to the Word For Developers forum if you so desire.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my webpage under my profile but do not reply to forum questions.
    Microsoft Developer tools
    Developer’s Guide to Windows 10 video series

    Friday, October 16, 2015 10:53 AM
  • You are aware the Word has an existing system of templates and auto-text?
    You can even make a serial letters from access DB or excel table, wich sounds about what you are trying to re-invent here in C#.
    And it is never a good idea to reinvent the wheel, if you got a fully tested car standing there, keys in the ignition :)

    Regarding: .docx or .doc in C# solution?

    If you need to only support .docx, you can use the OpenXML SDK without any special considerations (regarding where this can run). Or the ZipArchive and XMLWriter clases. That format is the easy case by far.

    If you need to support .doc in addtion, you have to use the (t)rusty COM based Word Interop.
    Wich requries office to be installed, can't run from a service or webserver (no interactive session) and invovles all the pitfalls of working with .COM

    • Proposed as answer by Kristin Xie Friday, October 23, 2015 9:04 AM
    • Marked as answer by Kristin Xie Monday, October 26, 2015 2:01 AM
    Friday, October 16, 2015 11:58 AM
  • its doc and docx both. and sorry for the bad explanation in Question i want to copy the Complte content of word document into another word Document.

    RehaanKhan. M

    Hi, here is an solution based on Spire.Doc Community Edition, both doc and docx are supported. It would probably help you.

    using Spire.Doc;
    
    namespace CopyContent
    {
        class Program
        {
            static void Main(string[] args)
            {
                //load source file
                Document sourceDoc = new Document("source.docx");
                //load destination file
                Document destinationDoc = new Document("destination.docx");         
                //copy content from source file and insert them to another file
                foreach (Section sec in sourceDoc.Sections) 
                {
                    foreach (DocumentObject obj in sec.Body.ChildObjects)
                    {
                        destinationDoc.Sections[0].Body.ChildObjects.Add(obj.Clone());
                    }
    
                }
                //save changes
                destinationDoc.SaveToFile("destination.docx");
            }
        }
    }

    Wednesday, November 18, 2015 2:25 AM