CDO keeps reformatting file

Posted on 2006-03-29
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("") = 2                   ' Use this mode in conjunction with the next line.
        .Item("") = GetServerNameSMTP()
    End With
    With iMsg
        Set .Configuration = iConf
        .To = ""               ' DON"T FORGET TO CHANGE HERE
        .cc = ""
        .bcc = ""
        .FROM = ""             ' 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?

Question by:access_dude
    LVL 15

    Accepted Solution

    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.
    LVL 5

    Assisted Solution

    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

    Author Comment

    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.
    LVL 5

    Expert Comment

    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

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now