Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7590387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:23:17+00:00 2026-05-30T20:23:17+00:00

With Visual Basic, I’m confused with that the behavior of Print statement in that

  • 0

With Visual Basic, I’m confused with that the behavior of Print statement in that sometimes the following statement: would cause additional carriage return “^M” at the end of a line, but sometimes, it doesn’t. I wondering why?

filePath = "d:\tmp\FAE-IMM-Report-2012-Week.org"
    If Dir(filePath) <> "" Then
        Kill filePath
    End If
    outFile = FreeFile()
    Open filePath For Output As outFile
    Print #outFile, "#+TITLE:     Weekly Report"

would produce

#+TITLE:     Weekly Report^M

while I wish without ^M:

#+TITLE:     Weekly Report

In one test program, almost the same code would produce no “^M”.

Please help! Thanks a lot.

Upon further experiment, I found that the following suggestion using vbNewline and “;” at the end of print content, still does not solve my problem.

After careful isolation, I found the cause of the problem is an character that seems like a space, not exactly space, followed by newline and carriage return. Before printing the text containing the offending string, there was no carriage return, but once the offending line is printed, then every line including the previous line printed would have carriage return.

I’m not sure what the exact the offending string is as my skill of VBA is not yet too well.

Here is a copy of the offending text from a spreadsheet cell:

   "There is something invisible after this visible text 
After the invisible text, then there might be a carriage return $Chr(13) and/or newline"

I’m not sure if the paste to web browser would preserve the content, though. By pasting to emacs, I did not see carriage return, while emacs should display it, if there is one. So I guess that there is no carriage return in the offending string.

Below is the program demonstrate the problem:

    Sub DemoCarriageReturnWillAppear()
    Dim filePath As String
    Dim outFile
    Dim offendingText
    filePath = "d:\tmp\demoCarriageReturn.org"
    If Dir(filePath) <> "" Then
        Kill filePath
    End If
    outFile = FreeFile()
    Open filePath For Output As outFile
    Print #outFile, "#+AUTHOR:    Yu Shen" & vbNewLine;
    Close #outFile 'At this moment, there is no carriage return
    Open filePath For Append As outFile
    offendingText = ThisWorkbook.Worksheets("Sheet1").Range("A1")
    Print #outFile, offendingText & vbNewLine;
    Close #outFile 'Now, every line end has carriage return.
    'It must be caused by something offending at the above print out content.
End Sub

Here is the final result of the above procedure:

    #+AUTHOR:    Yu Shen^M

There is something invisible after this visible text 
After the invisible text, then there might be a carriage return $Chr(13) or newline^M

Note the above “^M” is added by me, as carriage return would not be visible in browser.

If you’re interested, I can send you the excel file with the offending content.

I need your help on how to avoid those offending string, or the carriage returns.
(I even try to do string Replace of the carriage return or new line, as I found that once I manually deleted whatever caused change to another line, the problem would be gone. But calling Replace to replace vbNewline, Chr$(13), or vbCrLf did not make any difference.

Thanks for your further help!

Yu

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:23:18+00:00Added an answer on May 30, 2026 at 8:23 pm

    To help the other people in the future, here is an summary of my problem and the solution. The extra carriage return on each line even with semi-colon at the print statement end was actually caused by a string of space followed by newline (Chr$(A)) in one of the print statement, once such string is printed, then all previous and subsequent printed content would have an extra carriage return!

    It seems a bug on VBA 6 (with Excel 2007), a nasty one!

    My work-around was to replace the newline by a space.

    Thanks for Tony’s repeated help enabling me finally nailed down the cause.

    Here is the code to demonstrate the problem:

    Sub DemoCarriageReturnWillAppearOnAllLines()
        Dim filePath As String
        Dim outFile
        Dim offendingText
        filePath = "d:\tmp\demoCarriageReturn.org"
        If Dir(filePath) <> "" Then
            Kill filePath
        End If
        outFile = FreeFile()
        Open filePath For Output As outFile
        Print #outFile, "#+AUTHOR:    Yu Shen" & vbNewLine;
        Close #outFile 'At this moment, there is no carriage return
        Open filePath For Append As outFile
        offendingText = " " & Chr$(10)
        Print #outFile, offendingText & vbNewLine;
        Close #outFile 'Now, every line end has carriage return.
        'It must be caused by the offending at the above print out content.
    End Sub
    

    After the first “Close #outFile”, here is the content of the file demoCarriageReturn.org:

    #+AUTHOR:    Yu Shen
    

    Note: with editor capable showing carriage return as visible ^M, there is no carriage return present.

    However, after the second “Close #outFile”, here is the content of the same file with additional content:

    #+AUTHOR:    Yu Shen^M
    
    ^M 
    

    Note: there are two carriage returns appear. They are not intended. Especially, to the first line, the print statement has been executed, and at the previous close statement, it was found without carriage return. (To illustrate carriage return, I have to typing ^M in web page here. But it’s in the file of the print out.)

    This is why I think that it’s a bug, as the carriage returns are not intended. It’s undesirable surprise.

    The following code shows that if I filter out the linefeed character the problem would be gone.

    Sub DemoCarriageReturnWillNotAppearAtAll()
        Dim filePath As String
        Dim outFile
        Dim offendingText
        filePath = "d:\tmp\demoCarriageReturn.org"
        If Dir(filePath) <> "" Then
            Kill filePath
        End If
        outFile = FreeFile()
        Open filePath For Output As outFile
        Print #outFile, "#+AUTHOR:    Yu Shen" & vbNewLine;
        Close #outFile 'At this moment, there is no carriage return
        Open filePath For Append As outFile
        offendingText = " " & Chr$(10)
        Print #outFile, Replace(offendingText, Chr$(10), "") & vbNewLine;
        Close #outFile 'Now, no more carriage return.
        'The only change is removing the linefeed character in the second print statement
    End Sub
    

    After full execution of the above program, there is indeed no carriage return!

    #+AUTHOR:    Yu Shen
    

    This shows that string combination of space followed by linefeed caused the bug, and removing linefeed can avoid the bug.

    The following code further demonstrate that if there is no offending string, even without newline and semi-colon at the end of print statement, there would not be undesired carriage return!

    Sub DemoCarriageReturnWillNotAppearAtAllEvenWithoutNewLineFollowedBySemiColon()
        Dim filePath As String
        Dim outFile
        Dim offendingText
        filePath = "d:\tmp\demoCarriageReturn.org"
        If Dir(filePath) <> "" Then
            Kill filePath
        End If
        outFile = FreeFile()
        Open filePath For Output As outFile
        Print #outFile, "#+AUTHOR:    Yu Shen"
        Close #outFile 'At this moment, there is no carriage return
        Open filePath For Append As outFile
        offendingText = " " & Chr$(10)
        Print #outFile, Replace(offendingText, Chr$(10), "")
        Close #outFile 'Now, no more carriage return.
        'The real change is removing the linefeed character in the second print statement
    End Sub
    

    Also in the output result:

    #+AUTHOR:    Yu Shen
    

    Still no the annoying carriage return!

    This shows that using newline followed by semi-colon at the end of print statement is not the solution to the problem of carriage return at every line! The real solution is to avoid any string of space followed by linefeed in the print out content.

    Yu

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code is visual basic, .NET, ASP... all of the above? Can anybody
I'm using Visual Basic 9 (VS2008) and TagLib. The following code extracts the album
I'm using Visual Basic 2010 Express. I have a form that can be minimized.
I would like a Visual Basic for Application Function which shows the path of
Im new to visual basic.. I would like to ask on how to fixed
Visual Basic 6 sometimes tries to install or complete the installation of other applications
In a Visual Basic 6.0 program, I have a string sTemp that I want
I have the following Visual Basic 6.0 function which writes an ANSI string to
In Visual Basic 2008, there's two different ways that I know of to accomplish
I have a Visual Basic application that needs to round a number down, for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.