954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Retrieve texts from Word or text documents

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!

markdean.expres
Junior Poster
152 posts since Aug 2010
Reputation Points: 10
Solved Threads: 1
 

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", _
                  Word.WdSaveFormat.wdFormatText)

    docWord.Close()
    appWord.Quit()

    Dim txtFileName As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments & _
                                "\EffectiveConservative.txt"

    rtb1.LoadFile(txtFileName, RichTextBoxStreamType.PlainText)

  End Sub
  End Sub
End Class


That should Solve your problem

Phasma
Junior Poster in Training
81 posts since Nov 2008
Reputation Points: 21
Solved Threads: 21
 

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?

markdean.expres
Junior Poster
152 posts since Aug 2010
Reputation Points: 10
Solved Threads: 1
 

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

    Me.Close()
  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

Phasma
Junior Poster in Training
81 posts since Nov 2008
Reputation Points: 21
Solved Threads: 21
 

O definitely, I'll be marking this thread as solved... A HUGE thanks to you

markdean.expres
Junior Poster
152 posts since Aug 2010
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You