We help IT Professionals succeed at work.

CDO keeps reformatting file

access_dude asked
Medium Priority
Last Modified: 2010-05-01
The following code creates a file called "TESTFILE.TXT" and emails to a recipient.  Don't forget to change .TO and .FROM

Public Sub TEST()
    Open "C:\TESTFILE.TXT" For Output As #1    ' Open file for output.
    Print #1, "Line 1"    ' Print text to file.
    Print #1, "Line 2"
    Close #1

    Dim iMsg As Object
    Dim iConf As Object
    Set iMsg = CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")
    With iConf.Fields
        .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2                   ' Use this mode in conjunction with the next line.
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = GetServerNameSMTP()
    End With
    With iMsg
        Set .Configuration = iConf
        .To = "YourEmail@hotmail.com"               ' DON"T FORGET TO CHANGE HERE
        .cc = ""
        .bcc = ""
        .FROM = "YouEmail@hotmail.com"             ' DON"T FORGET TO CHANGE HERE
        .Subject = "Subject"
        .TextBody = "Body"
        .AddAttachment "C:\TESTFILE.TXT"
    End With
End Sub

If I send the file using mouse and keyboard it works fine on the receipt of attachment.  When I use the code above to send, the attachment goes through with screwed up CRLF's  i see a square when open the received attachment in notepad.

Expected contents on received file

Line 1
Line 2

Observed contents on received file (the [] is one square character probably a ascii 10 or a ascii13 only instead of both)


QUESTION: How do i get CDO to NOT screw up the CRLF's?

Watch Question

Try this instead

    Print #1, "Line 1" & vbCRLF & "Line 2"

In other words, you're using Print only once.

Internet e-mail standards forbid the presence of bare linefeed characters in e-mail messages. If you are sure to use carriage return before linefeed, this may take care of this issue. I believe something is happening in the creation but this would be a good way to test it.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
It's not CDO that is screwing it up.....its your Print to file commands

You might get more control using MS scripting runtime library

Dim objFSO As New FileSystemObject
Dim objTS As TextStream

Set objTS = objFSO.OpenTextFile("C:\TESTFILE.TXT", ForAppending, True)
objTS.WriteLine "Line1"
objTS.WriteLine "Line2"
Set objTS = Nothing
Set objFSO = Nothing


Tried this in our office and both methods worked fine.  I wonder if this may have something to do with their SMTP email server?  They are not using exchange.  I'm going to need a few days (or weeks) to check this out on the clients.  If the scriping library approach works at the client's then the points are yours.

as per Access_dudes last comment "Tried this in our office and both methods worked fine."

So it appears we both gave correct answers, however I don't know whether or not he got to try out the MS Scripting library approach?
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.