title: Avoid extra "carriage return" in Print statement with Visual Basic?
body: <p>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?</p>
  
  <pre><code>filePath = "d:\tmp\FAE-IMM-Report-2012-Week.org"
      If Dir(filePath) &lt;&gt; "" Then
          Kill filePath
      End If
      outFile = FreeFile()
      Open filePath For Output As outFile
      Print #outFile, "#+TITLE:     Weekly Report"
  </code></pre>
  
  <p>would produce</p>
  
  <pre><code>#+TITLE:     Weekly Report^M
  </code></pre>
  
  <p>while I wish without ^M:</p>
  
  <pre><code>#+TITLE:     Weekly Report
  </code></pre>
  
  <p>In one test program, almost the same code would produce no "^M". </p>
  
  <h2>Please help! Thanks a lot.</h2>
  
  <p>Upon further experiment, I found that the following suggestion using vbNewline and ";" at the end of print content, still does not solve my problem. </p>
  
  <p>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. </p>
  
  <p>I'm not sure what the exact the offending string is as my skill of VBA is not yet too well. </p>
  
  <p>Here is a copy of the offending text from a spreadsheet cell:</p>
  
  <pre><code>   "There is something invisible after this visible text 
  After the invisible text, then there might be a carriage return $Chr(13) and/or newline"
  </code></pre>
  
  <p>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. </p>
  
  <p>Below is the program demonstrate the problem:</p>
  
  <pre><code>    Sub DemoCarriageReturnWillAppear()
      Dim filePath As String
      Dim outFile
      Dim offendingText
      filePath = "d:\tmp\demoCarriageReturn.org"
      If Dir(filePath) &lt;&gt; "" Then
          Kill filePath
      End If
      outFile = FreeFile()
      Open filePath For Output As outFile
      Print #outFile, "#+AUTHOR:    Yu Shen" &amp; 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 &amp; 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
  </code></pre>
  
  <p>Here is the final result of the above procedure:</p>
  
  <pre><code>    #+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
  </code></pre>
  
  <p>Note the above "^M" is added by me, as carriage return would not be visible in browser. </p>
  
  <p>If you're interested, I can send you the excel file with the offending content. </p>
  
  <p>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. </p>
  
  <p>Thanks for your further help!</p>
  
  <p>Yu</p>