CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2000
    Posts
    27

    COnverting .rtf file to .txt file

    Hi,
    I tried to write a program for converting rich text files in my local directory into text files.
    After I finish running my program, the extension of files were changed to .txt. However, I used notepad to open those files, and there were a lot of unknown data instead of correct text.

    I will attach my program here for help. I will very appreciated any help.

    Sub FileExtension()

    Dim filepath As String
    Dim filertf As String
    Dim filetxt As String

    filepath = "C:\temp\"
    filertf = Dir(filepath & "*.rtf")

    While filertf <> ""
    filetxt = Left$(filertf, Len(filertf) - 3) & "txt"
    Name (filepath & filertf) As (filepath & filetxt)
    filertf = Dir

    Wend

    End Sub

    Thank you.




  2. #2
    Join Date
    Jan 2000
    Location
    MO, USA
    Posts
    1,506

    Re: COnverting .rtf file to .txt file

    rtf files have extra RTF code in them to force the font, color and other text display properties. the easiest way i can think of is to load the into an RichTextBox control and then grab the text property and save that as the original file name with a .txt extension.

    something like this:

    'have a form with a rich text box control name rtfMain on it
    'set a reference to the Microsoft Scripting Runtime library
    sub Main()
    Dim FSO as Scripting.FileSystemObject
    Dim oFolder as Scripting.Folder
    Dim oFile as Scripting.TextStream
    Dim Filer as Scripting.File

    set FSO = new Scripting.FileSystemObject

    set oFolder = FSO.GetFolder("C:\Program Files\Microsoft Office\Office\Headers")
    for Each Filer In oFolder.Files
    'check that we are trying to use an RTF file
    If UCase(Right(Filer.Name, 3)) = "RTF" then
    'load the file in the rich text box
    RichTextBox1.LoadFile Filer.Path, rtfRTF
    'now create a new file with a .txt extension
    set oFile = FSO.CreateTextFile(oFolder.Path & "\" & Left(Filer.Name, InStr(1, Filer.Name, ".")) & "txt", true, false)
    'write the plain text to the new file
    oFile.Write RichTextBox1.Text
    'save the file
    oFile.Close
    End If
    next

    set oFile = nothing
    set Filer = nothing
    set FSO = nothing




    that should do it for you.

    hope this helps,

    John

    John Pirkey
    MCSD
    http://www.ShallowWaterSystems.com
    http://www.stlvbug.org
    John Pirkey
    MCSD (VB6)
    http://www.stlvbug.org

  3. #3
    Join Date
    Jul 2000
    Posts
    27

    Re: COnverting .rtf file to .txt file

    John:
    Thank you for your help.
    Since the project is very urgent, and i have to make it work as soon as possible. And, the user only need the program running under Microsoft word as Macro. So I just wonder if there is any simple way to solve my problem?
    If you have any other suggestion, I will very appreciated.

    Allen


  4. #4
    Join Date
    Aug 2000
    Location
    KY
    Posts
    766

    Re: COnverting .rtf file to .txt file

    I see two options
    1.) Word has no trouble reading an RTF file
    2.) Create a form with a common dialog and a Rich text box control. Use the following code to strip out the RTF codes

    CommonDialog1.ShowOpen
    RichTextBox1.FileName = CommonDialog1.FileName
    RichTextBox1.SaveFile CommonDialog1.FileName, rtfText







  5. #5
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: COnverting .rtf file to .txt file

    The Richtextbox has both LOADFILE and SAVEFILE methods
    Simply do a

    Richtextbox1.SaveFile "filename", rtfText

    to save a rtf file in text format.
    NOTE: This will lose all RTF formatting, coloring, font controls etc.
    Also Not, The RichTextbox control is not normally visible in the Tolbox. To add it, Go to Project / Components and Select Microsoft Rich Text box.....
    for inclusion in the Toolbox.

    A quick example. Put a RichTextbox on a form and add two command buttons (Command1 and Command2)

    Paste the following code into the general declarations section of the form. Be sure to change the filenames in both command button_Click events

    Private Sub Command1_Click()
    RichTextBox1.LoadFile "C:\Our stuff\Jumptoit\Readme.rtf", rtfRTF

    End Sub

    Private Sub Command2_Click()
    RichTextBox1.SaveFile "C:\VB Stuff\VB Things to Look At\RTFtoTXT.txt", rtfText

    End Sub


    The result will be a converted RTF file


    John G

  6. #6
    Join Date
    Jul 2000
    Posts
    27

    Re: COnverting .rtf file to .txt file

    Hi,
    Thank you for your help.
    Actually, the user only want to see all text in notepad after the file is converted from .rtf to .txt.
    Right now, my code can only change the extension of files (from .rtf to .txt). The contents of file is still same. So that it appears a lot of rtf code when the user tries to open the file in notepad.

    Do you have any good idea about this issue?

    Thanks


  7. #7
    Join Date
    Aug 2000
    Location
    KY
    Posts
    766

    Re: COnverting .rtf file to .txt file

    if you add a richtextbox control and modify your code you should be Ok. The richtextbox has the ability to save to a file in text mode. So if you open the file and then put it in the richtextbox it will be in RTF format. When you issue the save method of the richtextbox there is an option to save the RTF file to a regular text file.
    if you look at the help file for the Save method you should see your options.


    While filertf <> ""
    filetxt = Left$(filertf, len(filertf) - 3) & "txt"
    'new Code Here
    richtextbox1.filename = filetxt
    richtextbox1.save filetxt ,rtftext
    'Remark this code out
    'Name (filepath & filertf) as (filepath & filetxt)
    'filertf = Dir





  8. #8
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: COnverting .rtf file to .txt file

    There is no magical solution to your problem. One thing is certain, You are going to have to run the .rtf file through a converter program of some sort. The actual file contains a bunch of gibberish (to you and me) that must be dealt with.
    The Loadfile and SaveFile methods I told you about are about the simplest ways I know of to accomplish this. One solution, not recommended, is to replace the Notepad.exe with your program that uses a invisible Rich textbox, loads the file into it then copies the text to a normal textbox and presents the normal textbox to the user.

    Here is a very simple program that will read a .rtf file and display the text only in a text box.

    Do this
    1).Start a new project.
    2).Make the window about 1/2 the size of your display.
    3).Add a normal Textbox on the top half of the window. Set its Multiline property = True
    4).Add a command button somewhere at the bottom of the form.
    5).Add a Richtext box on the bottom half (This is for demo purposes only). Later just make it invisible. It does not have to be seen to work.

    6).Add the following code to the form
    7).Change the File name to one of your liking.

    Private Sub Command1_Click()
    RichTextBox1.LoadFile "C:\Our stuff\Jumptoit\Readme.rtf"
    Text1.Text = RichTextBox1.Text
    End Sub

    8).Run the program and
    9).Click the command button.

    You should see the file displayed in the RTB and the pure text only in the textbox.

    John G

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured