Results 1 to 6 of 6

Thread: [RESOLVED] [Word] Remove Trailing vbCrLf in txt file

  1. #1

    Thread Starter
    Fanatic Member joltremari's Avatar
    Join Date
    Sep 2000
    Location
    Mississippi
    Posts
    674

    Resolved [RESOLVED] [Word] Remove Trailing vbCrLf in txt file

    Code:
    Open strPath For Input As #1 
     
    While EOF(1) = False 
        Line Input #1, strLine 
        If Right(strLine, 2) = "WX" Then 
             'Test and manipulate data here
             'Then format and write new strLine to strData
            strLine2 = Left(strLine, 7) 
            strLine3 = Trim(strLine2) 
            strData = strData & vbCrLf & strLine3 '<--problem here
        Else 
            strData = strData & vbCrLf & Trim(strLine) '<--problem here
        End If 
    Wend 
    Close #1 
     
     'Later, write to file
     
    Open strPath2 For Output As #1 
    Print #1, strData 
    Close #1
    The problem is that I end up with an extra vbCrLf at the end, and I can't seem to get rid of it. I need to figure out either, how to remove it before the file is written, or, how to keep it from writing a vbCrLf to the last line in the first place. Any suggestions welcome...

    Thanks,
    JO


    *Additional Info: This VBA in Word 2013, I am manipulating the data line by line before it gets written to the variable and ultimately to the file. Because I am using vbCrLf before writing each new string I end up with an extra at the end. This file gets passed on to another department and they've asked for the extra vbCrLf be removed prior to sending, as it is causing them some sort of problem.
    "I have not failed. I've just found 10,000 ways that won't work."
    'Thomas Edison'

    "If we knew what it was we were doing it wouldn't be called research, would it?"
    'Albert Einstein'

    VB6

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [Word] Remove Trailing vbCrLf in txt file

    try changing to
    Code:
    Print #1, strData;
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Fanatic Member joltremari's Avatar
    Join Date
    Sep 2000
    Location
    Mississippi
    Posts
    674

    Re: [Word] Remove Trailing vbCrLf in txt file

    Thanks, but no change. Curious though, what does the semicolon do?
    "I have not failed. I've just found 10,000 ways that won't work."
    'Thomas Edison'

    "If we knew what it was we were doing it wouldn't be called research, would it?"
    'Albert Einstein'

    VB6

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [Word] Remove Trailing vbCrLf in txt file

    prints without trailing lfcr

    consecutive print statements ending with ; will print on the same line

    if there could be multiple crlf at the end of the string, you can make a function like
    Code:
    Function remcrlf(s As String) As String
    Do
    If Right(s, 2) = vbCrLf Then
        s = Left(s, Len(s) - 2)
        Else
        Exit Do
    End If
    Loop
    remcrlf = s
    
    End Function
    to remove any number of crlf at the end of the string
    call like
    Code:
    Print #1, remcrlf(strData);
    Last edited by westconn1; Nov 6th, 2015 at 11:22 PM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Re: [Word] Remove Trailing vbCrLf in txt file

    thanks

  6. #6

    Thread Starter
    Fanatic Member joltremari's Avatar
    Join Date
    Sep 2000
    Location
    Mississippi
    Posts
    674

    Re: [Word] Remove Trailing vbCrLf in txt file

    Many thanks, that got me rolling.

    I noticed I had some rogue spaces in the file as well, so after handling them, added your function, I'm fixed!

    Thanks again,
    JO
    "I have not failed. I've just found 10,000 ways that won't work."
    'Thomas Edison'

    "If we knew what it was we were doing it wouldn't be called research, would it?"
    'Albert Einstein'

    VB6

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