dcsimg
Results 1 to 3 of 3

Thread: [RESOLVED] Outlook Macro to Copy Emails - Remove Carriage Return

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2019
    Posts
    2

    Resolved [RESOLVED] Outlook Macro to Copy Emails - Remove Carriage Return

    I am trying to create a macro that will loop through the emails in a specific folder and create a new TXT file with the contents of the email. I have the file creation and copying working as expected. However, each line that is written from the email into the text file ends with a carriage return + a line feed (I can see that by turning on special characters in notepad++). I need the files to just end with a line feed. I have been trying to use the replace function to remove this but haven't had any luck. I would appreciate if anyone has advice on how to get the carriage return removed.

    Content of email looks like this:
    ***START LINE***
    <content of email>
    ***END LINE***

    Code I'm using:
    Set inbox = ns.GetDefaultFolder(olFolderInbox).Folders("Enter Folder Here")

    For l = inbox.Items.Count To 1 Step -1
    Set item = inbox.Items(l)
    If TypeOf item Is MailItem Then

    'Extract org name from subject
    orgNameStart = InStr(1, item.Subject, "***") + 3
    orgNameEnd = InStr(orgNameStart, item.Subject, "***")
    orgName = Mid(item.Subject, orgNameStart, orgNameEnd - orgNameStart)
    orgNameRow = 0

    Open strPath & orgName & ".txt" For Output As #1

    resultLine = ""
    strCol = item.Body
    line = Split(strCol, vbCrLf)
    For i = LBound(line) To UBound(line)
    If line(i) = "***END LINE***" Then
    resultLine = ""
    End If
    If line(i) = "" Then
    End If
    If resultLine = "Yes" And line(i) <> "" Then
    writeLine = Replace(line(i), vbNewLine, vbLf)
    Print #1, writeLine
    End If
    If line(i) = "***START LINE***" Then
    resultLine = "Yes"
    End If
    Next i

    Close #1


    For the replace piece, I have also tried using these for the second parameter: vbCr, cvCrLf, Chr(13), and ASC(13)

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,510

    Re: Outlook Macro to Copy Emails - Remove Carriage Return

    print puts a crlf, so try
    Code:
    Print #1, writeLine;
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2019
    Posts
    2

    Resolved Re: Outlook Macro to Copy Emails - Remove Carriage Return

    Quote Originally Posted by westconn1 View Post
    print puts a crlf, so try
    Code:
    Print #1, writeLine;
    You are amazing; this did the trick. Thank you so much!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width