Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Read .rtf file and save as .txt

Read .rtf file and save as .txt

Scheduled Pinned Locked Moved Visual Basic
csharpcomsysadminhelp
6 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anoop Brijmohun
    wrote on last edited by
    #1

    Hi guys, i have a vb.net application that watched a folder and whenever a rtf file is dropped i want to read the text an save it to a txt file. here's my code

    Dim word_app As New Microsoft.Office.Interop.Word.Application
    Dim word_doc As New Microsoft.Office.Interop.Word.Document

    word_doc= word_app.Documents.Open(in_file, False, True, False, , , , , ,Microsoft.Office.Interop.Word.WdOpenFormat.wdOpenFormatRTF)
    word_doc.ActiveWindow.Selection.WholeStory()
    word_doc.ActiveWindow.Selection.Copy()
    str = My.Computer.Clipboard.GetData(GetType(String).ToString)
    'word_doc.SaveAs(out_file, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatText)

    doc.Close(False)
    word_server.Quit()

    Dim sr As New IO.StreamWriter(out_file, False)
    sr.Write(str)
    sr.Close()

    this is running on the a windows 2008 rc2 server as a window service. the error messaage is "Object reference not set to an instance of an object" if there is another simpler way please advise. thank you kindly Anoop :confused:

    J L P 3 Replies Last reply
    0
    • A Anoop Brijmohun

      Hi guys, i have a vb.net application that watched a folder and whenever a rtf file is dropped i want to read the text an save it to a txt file. here's my code

      Dim word_app As New Microsoft.Office.Interop.Word.Application
      Dim word_doc As New Microsoft.Office.Interop.Word.Document

      word_doc= word_app.Documents.Open(in_file, False, True, False, , , , , ,Microsoft.Office.Interop.Word.WdOpenFormat.wdOpenFormatRTF)
      word_doc.ActiveWindow.Selection.WholeStory()
      word_doc.ActiveWindow.Selection.Copy()
      str = My.Computer.Clipboard.GetData(GetType(String).ToString)
      'word_doc.SaveAs(out_file, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatText)

      doc.Close(False)
      word_server.Quit()

      Dim sr As New IO.StreamWriter(out_file, False)
      sr.Write(str)
      sr.Close()

      this is running on the a windows 2008 rc2 server as a window service. the error messaage is "Object reference not set to an instance of an object" if there is another simpler way please advise. thank you kindly Anoop :confused:

      J Offline
      J Offline
      Jay Royall
      wrote on last edited by
      #2

      Hi, the line which reads

      doc.Close(false)

      should that be

      word_doc.Close(false)

      And also, you have an object named word_server, where is that declared?

      A 1 Reply Last reply
      0
      • A Anoop Brijmohun

        Hi guys, i have a vb.net application that watched a folder and whenever a rtf file is dropped i want to read the text an save it to a txt file. here's my code

        Dim word_app As New Microsoft.Office.Interop.Word.Application
        Dim word_doc As New Microsoft.Office.Interop.Word.Document

        word_doc= word_app.Documents.Open(in_file, False, True, False, , , , , ,Microsoft.Office.Interop.Word.WdOpenFormat.wdOpenFormatRTF)
        word_doc.ActiveWindow.Selection.WholeStory()
        word_doc.ActiveWindow.Selection.Copy()
        str = My.Computer.Clipboard.GetData(GetType(String).ToString)
        'word_doc.SaveAs(out_file, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatText)

        doc.Close(False)
        word_server.Quit()

        Dim sr As New IO.StreamWriter(out_file, False)
        sr.Write(str)
        sr.Close()

        this is running on the a windows 2008 rc2 server as a window service. the error messaage is "Object reference not set to an instance of an object" if there is another simpler way please advise. thank you kindly Anoop :confused:

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        I suggest you forget about Word and experiment with a RichTextBox. You could set its Rtf property to load the RTF file content; then get its Text property. I haven't done this at any length, however the doc seems to suggest it would return the document content without any markup. BTW: IMO the RTB does not have to be visible, nor part of any Form for this. PS: As there are different levels of the RTF specification, it could happen that Word is more capable than an RTB, so complex documents might fail. I just don't know, I wish I did. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

        1 Reply Last reply
        0
        • J Jay Royall

          Hi, the line which reads

          doc.Close(false)

          should that be

          word_doc.Close(false)

          And also, you have an object named word_server, where is that declared?

          A Offline
          A Offline
          Anoop Brijmohun
          wrote on last edited by
          #4

          hi, sorry i tried to make it more readable for you guys. i seemed to have mis-renamed the var on this snippet. thanks

          1 Reply Last reply
          0
          • A Anoop Brijmohun

            Hi guys, i have a vb.net application that watched a folder and whenever a rtf file is dropped i want to read the text an save it to a txt file. here's my code

            Dim word_app As New Microsoft.Office.Interop.Word.Application
            Dim word_doc As New Microsoft.Office.Interop.Word.Document

            word_doc= word_app.Documents.Open(in_file, False, True, False, , , , , ,Microsoft.Office.Interop.Word.WdOpenFormat.wdOpenFormatRTF)
            word_doc.ActiveWindow.Selection.WholeStory()
            word_doc.ActiveWindow.Selection.Copy()
            str = My.Computer.Clipboard.GetData(GetType(String).ToString)
            'word_doc.SaveAs(out_file, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatText)

            doc.Close(False)
            word_server.Quit()

            Dim sr As New IO.StreamWriter(out_file, False)
            sr.Write(str)
            sr.Close()

            this is running on the a windows 2008 rc2 server as a window service. the error messaage is "Object reference not set to an instance of an object" if there is another simpler way please advise. thank you kindly Anoop :confused:

            P Offline
            P Offline
            Prerak Patel
            wrote on last edited by
            #5

            agreed, here is the function you can use..

            Sub convertRTFtoText(ByVal in_file_path As String, ByVal out_file_path As String)
            Dim rtb As New RichTextBox
            rtb.LoadFile(in_file_path) 'this is your rtf file path
            IO.File.WriteAllText(out_file_path, rtb.Text) 'this is your txt file path
            End Sub

            A 1 Reply Last reply
            0
            • P Prerak Patel

              agreed, here is the function you can use..

              Sub convertRTFtoText(ByVal in_file_path As String, ByVal out_file_path As String)
              Dim rtb As New RichTextBox
              rtb.LoadFile(in_file_path) 'this is your rtf file path
              IO.File.WriteAllText(out_file_path, rtb.Text) 'this is your txt file path
              End Sub

              A Offline
              A Offline
              Anoop Brijmohun
              wrote on last edited by
              #6

              thanks, i have used something similar.

              Me.RichTextBox1.LoadFile(in_file)

                      Dim sr As New IO.StreamWriter(out\_file, False)
                      sr.Write(Me.RichTextBox1.Text)
                      sr.Close()
              

              thanks guys! :)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups