Copy Content from one Word Document to another in C#, VB.NET

In our daily work, we often meet the requirements to copy parts or the whole content (without header or footer) from one Word document to another. It's an easy task if we use copy and paste function.

However, how could we achieve this task programmatically? This article is aimed to introduce the method of how to transfer the whole content from source document to target document using Spire.Doc. If you only want to transfer several paragraphs, please refer to this article.

Source Document:

Copy Content from one Word Document to another in C#, VB.NET

Target Document:

Copy Content from one Word Document to another in C#, VB.NET

Code Snippet:

Step 1: Initialize a new object of Document class and load the source document.

Document sourceDoc = new Document("source.docx");

Step 2: Initialize another object to load target document.

Document destinationDoc = new Document("target.docx");

Step 3: Copy content from source file and insert them to the target file.

foreach (Section sec in sourceDoc.Sections) 
{
    foreach (DocumentObject obj in sec.Body.ChildObjects)
    {
        destinationDoc.Sections[0].Body.ChildObjects.Add(obj.Clone());
    }
}

Step 4: Save the changes.

destinationDoc.SaveToFile("target.docx", FileFormat.Docx2010);

Result:

Copy Content from one Word Document to another in C#, VB.NET

Full Code:

[C#]
Document sourceDoc = new Document("source.docx");
Document destinationDoc = new Document("target.docx");         
foreach (Section sec in sourceDoc.Sections) 
{
    foreach (DocumentObject obj in sec.Body.ChildObjects)
    {
        destinationDoc.Sections[0].Body.ChildObjects.Add(obj.Clone());
    }
}
destinationDoc.SaveToFile("target.docx", FileFormat.Docx2010);
System.Diagnostics.Process.Start("target.docx");
[VB.NET]
Dim sourceDoc As New Document("source.docx")
Dim destinationDoc As New Document("target.docx")
For Each sec As Section In sourceDoc.Sections
	For Each obj As DocumentObject In sec.Body.ChildObjects
		destinationDoc.Sections(0).Body.ChildObjects.Add(obj.Clone())

	Next
Next
destinationDoc.SaveToFile("target.docx", FileFormat.Docx2010)
System.Diagnostics.Process.Start("target.docx")