Inserting or appending document or file in a word document programmatically using C#

Create a new windows application project and add a button to it.

On click of that button, we will create a new document and append or insert content of two documents in it.

First add reference to (Word 10.0 or 11.0 object library) within COM tab of Add reference dialog box.

After adding reference, add this directive

using Microsoft.Office.Interop.Word

Put this code on button click

// For optional parameters create a missing object

object missing = System.Reflection.Missing.Value; 

// Create an object of application class

ApplicationClass WordApp = new ApplicationClass();  

// add a document in the Application

Document adoc=WordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing); 

// declare variables for setting the position within the document

object start = 0;

object end = 0; 

// create a range object which starts at 0

Range rng = adoc.Range(ref start, ref missing);

// insert a file

rng.InsertFile(@”C:\MyFirst.doc”, ref missing, ref missing, ref missing, ref missing);

// now make start to point to the end of the content of the first document

start = WordApp.ActiveDocument.Content.End – 1; 

// create another range object with the new value for start

Range rng1 = adoc.Range(ref start, ref missing);

// insert the another document

rng1.InsertFile(@”C:\MySecond.doc”, ref missing, ref missing, ref missing, ref missing);

// now make start to point to the end of the content of the first document

start = WordApp.ActiveDocument.Content.End – 1; 

// make the word appliction visible

WordApp.Visible = true;

 

Bye


7 thoughts on “Inserting or appending document or file in a word document programmatically using C#

  1. I am trying to use the above code but getting the error message at the below line:
    rng.InsertFile(fileName, ref missing, ref missing, ref missing, ref missing);
    COMException was unhandled.
    Files are there but its not picking up.
    Below is my code. Please look in to it.

    if (Directory.Exists(newpath))
    {
    object missing = System.Reflection.Missing.Value;
    Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();
    Document adoc = WordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
    object start = 0;
    object end = 0;
    string[] files = Directory.GetFiles(newpath);
    foreach (string s in files)
    {
    fileName = Path.GetFileName(s);
    Range rng = adoc.Range(ref start, ref missing);
    rng.InsertFile(fileName, ref missing, ref missing, ref missing, ref missing);
    start = (WordApp.ActiveDocument.Content.End) – 1;
    }
    WordApp.Visible = true;
    }

  2. Hi,
    I have two docs.
    Is it possible each doc will be insert in new page and the same time i want keep 2 inch top margin.
    Please Advice me on this.
    Thanks

  3. Good article your website is the best for word programming,just keep going
    if you could fit a picture inside a word table cell
    it is what i am looking for

  4. Do you have VB.NET verision of Inserting or appending document or file in a word document programmatically. I was unable to convert the C# code to vb.net.

  5. Hi,

    I have two docs with three pages each.
    Is it possible to append pages 2&3 of the second doc after the 3rd page of the first doc. So I could end up with a 5 page doc.
    Please Advice me on this.
    Thanks

Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s