Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to open ".doc" & ".docx" file in richtextbox

What I have tried:

If DiscardChanges() Then
            OpenFileDialog1.Filter =
                "RTF Files|*.RTF|DOC Files|*.doc|" &
                "Text Files|*.TXT|All Files|*.*"
            If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
                'fName = OpenFileDialog1.FileName
                Editor.Text = My.Computer.FileSystem.ReadAllText(OpenFileDialog1.FileName)
                Editor.Modified = False
            End If
        End If

    End Sub
    Function DiscardChanges() As Boolean
        If Editor.Modified Then
            Dim reply As MsgBoxResult
            reply = MsgBox(
            "Text hasn’t been saved. Discard changes?",
            If reply = MsgBoxResult.No Then
                Return False
                Return True
            End If
            Return True
        End If
    End Function
Updated 24-Jun-19 3:41am

Not going to be trivial: RichTextBoxes accept plain text and Rich Text only - and neither DOC nor DOCX contain that. DOC is a proprietary binary format, DOCX is a Zipped XML format, and neither of those in any way resemble RTF data.

If you want to get Word data into a RichTextBox complete with formatting, you are looking at a fair amount of work, though it may be possible for you to "cheat" and use a genuine copy of Microsoft Word to read the DOC / DOCX file (which it can do) and save it as a temporary file in RTF format (which it can do) via Interop, then load the RTF into your control before deleting the temporary file. I don't in any way guarantee that it'll be formatted well though, I don't play with Word if I can avoid it.
And it's a horrible cludge.
Share this answer
First of all, please, read solution #1 by OriginalGriff[^]. He's right!

As far as RichTextBox[^] has Lines[^], an MS Word document[^] does not have lines! It has collection of Paragraphs[^].

A paragraph can contain text, shapes, images, etc. If you want to get via Interop clear-text only, you can use below method:

Imports Word = Microsoft.Office.Interop.Word

Dim sFileName As String = "fullfilename"
'open Word app
Dim wdApp As Word.Application = New Word.Application()
'open document
Dim wdDoc As Word.Document = wdApp.Documents.Open(sFileName)
'read paragraphs using Linq
Dim lines As String() = wdDoc.Paragraphs.Cast(Of Word.Paragraph) _
		.Select(Function(x) x.Range.Text.Trim()).ToArray()
'add lines
RichTextBox1.Lines = lines
'close the document

Do not forget to add reference to Microsoft.Office.Interop.Word.dll! How? c# - How to add reference for "Microsoft.Office.Interop.Word" in .net - Stack Overflow[^]

For further details, please see:
Documents.Open method (Word) | Microsoft Docs[^]
Working with Word document content objects - VB.NET code samples[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900