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


Discover more from Nishant Rana's Weblog

Subscribe to get the latest posts sent to your email.

Unknown's avatar

Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

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

  1. 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

    Like

  2. 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.

    Like

  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

    Like

  4. 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

    Like

  5. 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;
    }

    Like

Please share your thoughts

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Nishant Rana's Weblog

Subscribe now to keep reading and get access to the full archive.

Continue reading

Power Platform Puzzles

D365 CRM, Power Platform Tips &Tricks

Power Spark

Power Spark By Shrangarika

Van Carl Nguyen

Exploration of Power Platform

My Trial

It is my experience timeline.

Power⚡Thomas

Sharing my knowledge and experience about the Microsoft Power Platform.

Arpit Power Guide

a guide to powering up community

Welcome to the Blog of Paul Andrew

Sponsored by Cloud Formations Ltd

Deriving Dynamics 365

Deriving Solutions and features on Power Platform/Dynamics 365

The CRM Ninja

Thoughts & musings from a Microsoft Business Applications Ninja!

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog - PPUG.ORG

AI - Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue

The Professional Paraphraser

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

Power Platform Learning

Your Go-To Resource for Power Apps, Power Automate & More

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.