[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

CDO keeps reformatting file

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?

  • 2
2 Solutions
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.
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
access_dudeAuthor Commented:
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?

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now