Guys is it possible that I create a program whose functionality would be selecting a file, then gets the contents of that file (Word or text files). The program, of course, has a Richtextbox on it where the file content will be filled to.

If it is, could anyone please help!

5 Years
Discussion Span
Last Post by markdean.expres

Add a reference to Microsoft.Office.Interop.Word
Open the word doc and save it as a text file. Then load the text file into the RichTextBox.

Imports Microsoft.Office.Interop

Public Class Form1
  Dim appWord As New Word.Application
  Dim docWord As New Word.Document

  Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    docWord = appWord.Documents.Open(My.Computer.FileSystem.SpecialDirectories.MyDocuments & _
                                     "\Effective Conservative.docx")
    Dim document_Renamed As New Word.Document()

    docWord.SaveAs(My.Computer.FileSystem.SpecialDirectories.MyDocuments & _
                  "\EffectiveConservative.txt", _


    Dim txtFileName As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments & _

    rtb1.LoadFile(txtFileName, RichTextBoxStreamType.PlainText)

  End Sub
  End Sub
End Class

That should Solve your problem


Yes, this definitely has solved my problem, however, I forgot to include the saving part. How about if I want to make changes on the RichTextbox and save it. It should reflect on the textfile. is this also possible? How?


Add a global variable

Dim txtChanged As Boolean

A Save Button

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    If txtChanged Then
      My.Computer.FileSystem.WriteAllText(My.Computer.FileSystem.SpecialDirectories.MyDocuments & _
                                         "\EffectiveConservative.txt", rtb1.Text, False)
    End If

  End Sub

Using the TextChanged Event of the RichTextBox Set the txtChanged variable to true

Private Sub rtb1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rtb1.TextChanged
    txtChanged = True
  End Sub

This will save any edits to the text file. Saving back to a word Doc is a little more complicated, I have done it but, I get errors opening the word file then MS gives up and shows the updated document. Kind of sad it's not nearly as easy as say creating an excel file or access database. I cringe when it comes to programming word or outlook!!

BTW if this solves your problem please mark this as solved....Thanks

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.