找到"回车"在mail.body

时间:2015-07-30 07:38:43

标签: vba excel-vba outlook excel

我有这样的邮件:

  

您好,

     

请注意我们在16点15分做了

     

已完成的操作:重建等

     

真诚地

     

先生。

每个邮件中的操作都会发生变化,我想要的是在Excel中插入操作。问题是我不知道如何获得"回车" (如果这是正确的名字,这就是traduction给我的)。 我在互联网上发现的是vbLfChr(10)是"回车"。 我尝试的是找到开头:

TechnicPosition= InStr(1, .Body, "Actions done: ")
TechnicAction= Mid(.Body, TechnicPosition, 14) ' first char from the action

但是我无法获得最后一个字符(第一个"回车"来自TechnicAction)。 我尝试过很多东西:InStr(TechnicPosition, .Body, vbCrLf)

我的问题:如何获得一个从一个单词开始到一个"回车的句子" (第一个出现在开头的词之后)?

3 个答案:

答案 0 :(得分:3)

电子邮件正文中的carriage return通常为vbNewline

这就是我通常的做法

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

修改

ANOTHER WAY

答案 1 :(得分:0)

试试这个:

TechnicPosition = InStr(1, .Body, "Actions done: ")
TechnicEndPosition = InStr(TechnicPosition, .Body, Chr(10))
TechnicAction = Mid(.Body, TechnicPosition + 14, TechnicEndPosition - TechnicPosition - 14)

答案 2 :(得分:0)

遍历身体以查看角色是什么:

For i = 1 To Len(.Body)
    If Not Mid(.Body, i, 1) Like "[A-Za-z0-9,'?!"".:]" Then
        Debug.Print Asc(Mid(.Body, i, 1))
    End If
Next

找到Asc()值后,将主体拆分并返回第二个索引:

TechnicAction = Split(.Body, Asc(10))(1) '// Or whatever Asc() is required