excel - Outlook 电子邮件正文不会复制到 Excel

标签 excel vba outlook

以下代码有效它将从指定的电子邮件中打开指定的文件。但是它不会将正文消息分隔到excel中的不同行,有什么建议吗?

For i = LBound(MyAr) To UBound(MyAr)
    '~~> This will give you the contents of your email
    '~~> on separate lines
    Debug.Print MyAr(i)
Next i
    End With
Const xlUp As Long = -4162

Sub ExportToExcel(MyMail As MailItem)
    Dim strID As String, olNS As Outlook.NameSpace
    Dim olMail As Outlook.MailItem
    Dim strFileName As String

'~~> Excel Variables
Dim oXLApp As Object, oXLwb As Object, oXLws As Object
Dim lRow As Long

strID = MyMail.EntryID
Set olNS = Application.GetNamespace("MAPI")
Set olMail = olNS.GetItemFromID(strID)

'~~> Establish an EXCEL application object
On Error Resume Next
Set oXLApp = GetObject(, "Excel.Application")

'~~> If not found then create new instance
If Err.Number <> 0 Then
    Set oXLApp = CreateObject("Excel.Application")
End If
Err.Clear
On Error GoTo 0

'~~> Show Excel
oXLApp.Visible = True

'~~> Open the relevant file
Set oXLwb = oXLApp.Workbooks.Open("C:\Users\ltorres\Documents\multiplier.xlsx")

'~~> Set the relevant output sheet. Change as applicable
Set oXLws = oXLwb.Sheets("Sheet1")

lRow = oXLws.Range("A" & oXLws.Rows.Count).End(xlUp).Row + 1

'~~> Write to outlook
With oXLws
Dim MyAr() As String

MyAr = Split(olMail.Body, vbCrLf)

For i = LBound(MyAr) To UBound(MyAr)
    '~~> This will give you the contents of your email
    '~~> on separate lines
    Debug.Print MyAr(i)
Next i
    End With

'~~> Close and Clean up Excel
oXLwb.Close (True)
oXLApp.Quit
Set oXLws = Nothing
Set oXLwb = Nothing
Set oXLApp = Nothing

Set olMail = Nothing
Set olNS = Nothing

结束子

最佳答案

您可以设置lRowWith语句,但您还需要在每次出现 MyAr 定义的换行符时添加 1 行, 尝试:

With oXLws
lRow = .Range("A" & .Rows.Count).End(xlUp).Row + 1
Dim MyAr() As String
MyAr = Split(olMail.Body, vbCrLf)
For i = LBound(MyAr) To UBound(MyAr)
    .Range("A" & lRow).Value = MyAr(i)
    lRow = lRow + 1
Next i
End With

关于excel - Outlook 电子邮件正文不会复制到 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37189525/

相关文章:

delphi - 将邮件从 OutLook 拉到文件中

html - 修复时事通讯代码以在 Outlook 中正常工作

c# - 从 EWS 访问 Outlook 用户属性

node.js - 如何从 Node.js 程序运行 Excel VBA?

javascript - 谷歌浏览器 + JavaScript[jquery] : Blob to Excel

excel - 如何在数据透视表中选择 lastrow 之前的行?

vba - 在 VBA 中链接类

vba - [AWorrkbookName] VBA 语法是新语法还是旧语法?

php - 在excel上使用php进行数字签名

vba - 使用 VBA 从不同的工作表进行 Vlookup