How to Open word file in RichTextBox using C#

By FoxLearn 7/18/2024 3:41:59 AM   22.29K
To open a Word document into a RichTextBox control in a C# Windows Forms Application, you can use the Microsoft Office Interop libraries.

Here’s a step-by-step guide on how how to Open word file in RichTextBox in C#

To load word file in RichTextBox control in C# Winforms, you can use COM. This is a simple way to how to read (*.doc or *.docx) file into RichTextBox.

Creating a new Window Forms Application project, then open your form designer.

Next, Drag and drop the RichTextBox, Button controls from the Visual Studio Toolbox to your winform.

open word file in richtextbox c#

You can layout your ui as show above allows you to import (*.doc or *.docx) file to RichTextBox in C# .NET

Open your C# project in Visual Studio, then Right-click on "References" in the Solution Explorer -> Select "Add Reference...".

You need to add a reference to Microsoft.Office.Interop.Word.dll by right-clicking on your project, then select Add Reference...

reference manager

Clicking on Browse button, then browse to C:\Windows\assembly\GAC_MSIL\Microsoft.Office.Interop.Word

Next, select the Microsoft.Office.Interop.Word.dll inside your guid folder.

Finally, Add a click event handler to the Open button allows you to read a word (*.doc or *.docx) file, then import into RichTextBox control.

// c# read file docx
private void btnOpen_Click(object sender, EventArgs e)
{
    // c# read docx file
    using (OpenFileDialog ofd = new OpenFileDialog() { ValidateNames = true, Multiselect = false, Filter = "Word Doucment|*.docx|Word 97 - 2003 Document|*.doc" })
    {
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            object readOnly = true;
            object visible = true;
            object save = false;
            object fileName = ofd.FileName;
            object missing = Type.Missing;
            object newTemplate = false;
            object docType = 0;
            Microsoft.Office.Interop.Word._Document oDoc = null;
            // Open the Word document using Microsoft Office Interop Word
            Microsoft.Office.Interop.Word._Application oWord = new Microsoft.Office.Interop.Word.Application() { Visible = false };
            oDoc = oWord.Documents.Open(
                    ref fileName, ref missing, ref readOnly, ref missing,
                    ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref visible,
                    ref missing, ref missing, ref missing, ref missing);
            oDoc.ActiveWindow.Selection.WholeStory();
            oDoc.ActiveWindow.Selection.Copy();
            // c# read word document
            IDataObject data = Clipboard.GetDataObject();
            // Read the content and display in RichTextBox
            rtfText.Rtf = data.GetData(DataFormats.Rtf).ToString();
            // Close the Word document and release resources
            oDoc.Close();
            oWord.Quit(ref missing, ref missing, ref missing);
        }
    }
}

First, We will use COM to read word (*.doc or *.docx) file, then select all data in word file.

Second, We will copy data to Clipboard.

Finally, We will convert clipboard data to RTF data, then set rtf data to the RichTextBox control.

Ensure Microsoft Word is installed on the machine where your application will run because this approach uses interop with the Word application.

VIDEO TUTORIAL