Results 1 to 7 of 7
  1. #1
    2 Star Lounger
    Join Date
    Apr 2002
    Location
    Amersfoort, Utrecht, Netherlands
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts

    change CR to LF (vba 97)

    Hello all,

    I have a TXT file where I have to replace all CR to LF.
    Is it posible to use some code in an Office application to change this txt file?

    Thanks in advance.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: change CR to LF (vba 97)

    Here is a general VBA routine to do so. It can be run from Word, Excel or Access, or incorporated in a VB6 application. For testing purposes, the code will create a new file; test thorougly that the output is correct. When satisfied, you can uncomment two lines near the end (indicated in the code) to make the code replace the original file.

    Sub Cr2Lf(strFilename As String)
    Dim intIn As Integer
    Dim intOut As Integer
    Dim bytChar As Byte

    On Error GoTo ErrHandler

    intIn = FreeFile
    Open strFilename For Binary Access Read As #intIn
    intOut = FreeFile
    Open strFilename & "2" For Binary Access Write As #intOut

    Do While Not EOF(intIn)
    Get #intIn, , bytChar
    If bytChar = 13 Then bytChar = 10
    Put #intOut, , bytChar
    Loop

    Close #intOut
    Close #intIn
    ' Uncomment the following lines after testing
    'Kill strFilename
    'Name strFilename & "2" As strFilename

    ExitHandler:
    On Error Resume Next
    Close #intOut
    Close #intIn
    Exit Sub

    ErrHandler:
    MsgBox Err.Description, vbExclamation
    Resume ExitHandler
    End Sub

    Use as follows:

    Cr2Lf "C:TestTextfile.txt"

  3. #3
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: change CR to LF (vba 97)

    Just curious here Hans. Is it possible to use Word's search/ replace dialog to do this? That is, use ^0013 and ^0010 in the S/R dialog?

    Alan

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: change CR to LF (vba 97)

    Seems to work <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    Don't know if there are situations in which Word would mess up the file, but it looks OK in a quick test.

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: change CR to LF (vba 97)

    Another way would be to use VBScript from the command prompt. A demo is attached. You can use the VB Shell object to run batch files, but I'm not sure you could pass the file name parameter.

  6. #6
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: change CR to LF (vba 97)

    Word can't deal properly with ^10. You might be lucky if you immediately save in *.txt format again, but usually Word will replace ^10 (or ^13^10 = CrLf pairs) with ^13.

    Probably the other methods mentioned are safer.

    BTW: In Word2002/2003, there is an option when you save as text that allows you to end lines with ^13^10, ^13, ^10, or ^10^13.
    In Word97/2000, there was a file MSTXTCNV.INI, which allowed you to set a few options for how Word saved files in certain formats. But I don't remember anything about end-of-line characters in text files.

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16> Klaus

  7. #7
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: change CR to LF (vba 97)

    Thanks (Hans too) for the reminder of Word "doing its own thing" in certain capacities. I should have remembered how I used to use Word to reformat certain text files, simply by opening then resaving. I'd concur that the CRLF issue is a bit of a wildcard with Word.

    Alan

Posting Permissions

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