在 mail.body 中找到 "carriage return"
Find "carriage return" in a mail.body
我有这样的邮件:
Hello,
Please note we did ... at 16h15
Actions done: Rebuilding etc
sincerely
Mr.
每封邮件中的操作都会发生变化,我想要的是将操作插入我的 Excel。问题是我不知道如何获得 "carriage return" (idk 如果这是正确的名字,这就是 traduction 给我的)。
我在网上找到的是 vbLfChr(10)
是 "carriage return"。
我尝试的是找到开头:
TechnicPosition= InStr(1, .Body, "Actions done: ")
TechnicAction= Mid(.Body, TechnicPosition, 14) ' first char from the action
但我无法获取最后一个字符(来自 TechnicAction 的第一个 "carriage return")。
我尝试了很多事情,例如:InStr(TechnicPosition, .Body, vbCrLf)
我的问题:如何得到一个从单词开始的句子到 "carriage return"(第一个出现在开头单词之后的单词)?
试试这个:
TechnicPosition = InStr(1, .Body, "Actions done: ")
TechnicEndPosition = InStr(TechnicPosition, .Body, Chr(10))
TechnicAction = Mid(.Body, TechnicPosition + 14, TechnicEndPosition - TechnicPosition - 14)
遍历正文以查看字符是什么:
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()
值后,拆分正文和 return 第二个索引:
TechnicAction = Split(.Body, Asc(10))(1) '// Or whatever Asc() is required
邮件正文中的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
我有这样的邮件:
Hello,
Please note we did ... at 16h15
Actions done: Rebuilding etc
sincerely
Mr.
每封邮件中的操作都会发生变化,我想要的是将操作插入我的 Excel。问题是我不知道如何获得 "carriage return" (idk 如果这是正确的名字,这就是 traduction 给我的)。
我在网上找到的是 vbLfChr(10)
是 "carriage return"。
我尝试的是找到开头:
TechnicPosition= InStr(1, .Body, "Actions done: ")
TechnicAction= Mid(.Body, TechnicPosition, 14) ' first char from the action
但我无法获取最后一个字符(来自 TechnicAction 的第一个 "carriage return")。
我尝试了很多事情,例如:InStr(TechnicPosition, .Body, vbCrLf)
我的问题:如何得到一个从单词开始的句子到 "carriage return"(第一个出现在开头单词之后的单词)?
试试这个:
TechnicPosition = InStr(1, .Body, "Actions done: ")
TechnicEndPosition = InStr(TechnicPosition, .Body, Chr(10))
TechnicAction = Mid(.Body, TechnicPosition + 14, TechnicEndPosition - TechnicPosition - 14)
遍历正文以查看字符是什么:
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()
值后,拆分正文和 return 第二个索引:
TechnicAction = Split(.Body, Asc(10))(1) '// Or whatever Asc() is required
邮件正文中的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