Find “carriage return” in a mail.body

后端 未结 3 1547
傲寒
傲寒 2021-01-21 12:39

I have mails like this :

Hello,

Please note we did ... at 16h15

Actions done: Rebuilding etc

sincerely

3条回答
  •  终归单人心
    2021-01-21 13:21

    The carriage return in the email body is usually vbNewline

    This is how I usually do it

    Sub Sample()
        Dim sBody As String
        Dim MyAr
        Dim i As Long
    
        '
        '~~> Rest of your code
        '
    
        sBody = oMail.Body
    
        '~~> For testing purpose
        'sBody = "Hello," & vbNewLine & vbNewLine
        'sBody = sBody & "Please note we did ... at 16h15" & vbNewLine & vbNewLine
        'sBody = sBody & "Actions done: Rebuilding etc" & vbNewLine & vbNewLine
        'sBody = sBody & "Sincerely"
    
        '~~> Split the email body on vbnewline and store it in array
        MyAr = Split(sBody, vbNewLine)
    
        '~~> Loop through array
        For i = LBound(MyAr) To UBound(MyAr)
            '~~> Check if the line has "Actions done:"
            If InStr(1, MyAr(i), "Actions done:") Then
                '~~> This would give you "Rebuilding etc"
                '~~> Split the line on "Actions done:"
                '~~> You will get an array. Ar(0) will have "Actions done:"
                '~~> And Ar(1) will have what you are looking for so we use
                '~~> Split()(1) directly to access that item
                MsgBox Split(MyAr(i), "Actions done:")(1)
                Exit For
            End If
        Next i
    End Sub
    

    Edit

    ANOTHER WAY

提交回复
热议问题