Write data to Word document using .NET

here’s a sample code in C# using the .NET framework to write data to a Word document

using System;
using Microsoft.Office.Interop.Word;

namespace WordDocWriterExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Define the Word document path and name
            string filePath = @"C:\Users\UserName\Documents\example.docx";

            // Define the data to be written to the Word document
            string data = "Hello, world!";

            // Create a new instance of Word application
            Application wordApp = new Application();

            // Open the Word document
            Document wordDoc = wordApp.Documents.Open(filePath);

            // Set the cursor position to the end of the document
            object missing = System.Reflection.Missing.Value;
            object endOfDoc = "\\endofdoc";
            Range range = wordDoc.Bookmarks.get_Item(ref endOfDoc).Range;

            // Write the data to the Word document
            range.Text = data;

            // Save and close the Word document
            wordDoc.Save();
            wordDoc.Close();

            Console.WriteLine("Data has been written to the Word document.");
            Console.ReadLine();
        }
    }
}

In this example, the Application and Document classes from the Microsoft Office Interop Word library are used to create a new instance of Word application and open the Word document. The Range object is used to set the cursor position to the end of the document. The Text property of the Range object is used to write the data to the Word document. The Save and Close methods of the Document object are used to save and close the Word document.

Pamai Tech
Turning ideas into Reality

Products

Office Add-in

Enterprise Solutions

Cloud Consulting

UI UX Design

Data Transformation

Services

FAQ's

Privacy Policy

Terms & Condition

Team

Contact Us

Company

About Us

Services

Features

Our Pricing

Latest News

© 2025 Pamai Tech