Windows Forms: How to Open word file in RichTextBox using C#

This post shows you How to open the word document into RichTextBox in C# .NET Windows Forms Application.

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

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

Next, Drag 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

Next, 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.

private void btnOpen_Click(object sender, EventArgs e)
    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;
            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);
            IDataObject data = Clipboard.GetDataObject();
            rtfText.Rtf = data.GetData(DataFormats.Rtf).ToString();
            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.