将Outlook电子邮件信息访问到Excel工作表(VBA):body,HTMLbody,RTFbody是<>空

时间:2013-06-13 10:55:03

标签: excel email vba

提前感谢您的帮助...我的Google今天失败了。

我发现有很多关于使用excel VBA访问电子邮件信息的来源,并且在大多数情况下我可以让它们工作。但由于某种原因,最关键的部分,即电子邮件的正文,是空白的。

Dim myOlApp As Object, myNameSpace As Object
Set myOlApp = CreateObject("Outlook.Application")
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myfolders = myNameSpace.Folders
n = 1
Do Until myfolders.Item(n) = "xxxxx"
    n = n + 1
Loop
Set myfolder = myfolders.Item(n)
Set myfolder2 = myfolder.Folders("Inbox")
c = 12
n = 36
For Each Item In myfolder2.Items
    itsj = Item.Subject
    itrt = Item.ReceivedTime
    itbo = Item.Body
    Cells(n, c) = itrt
    Cells(n, c + 1) = itsj
    Cells(n, c + 2) = itbo
    n = n + 1
 Next Item

除了Item.Body之外,一切似乎都有效。我得到了

运行时错误“287”: 应用程序定义或对象定义的错误

查看.body,或者就此而言:.HTMLBody或.RTFBody,在Locals视图中,它们都显示了<>的值。我尝试过来自不同人的不同电子邮件,但结果总是相同。我还在Locals文件夹中钻取所有其他对象以直接引用电子邮件并获得相同的结果。我正在使用2010 Outlook和Excel。

谢谢!

1 个答案:

答案 0 :(得分:0)

我与我没有任何问题,除了它实际上没有在细胞中放任何东西。 所以,我只需触摸它就可以修改它并放入Application.ActiveSheet:

Dim myOlApp As Object, myNameSpace As Object
Set myOlApp = CreateObject("Outlook.Application")
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myfolders = myNameSpace.Folders
n = 1
Do Until myfolders.Item(n) = "xxxxx"
    n = n + 1
Loop
Set myfolder = myfolders.Item(n)
Set myfolder2 = myfolder.Folders("Inbox")
c = 1
n = 1
For Each Item In myfolder2.Items
    itsj = Item.Subject
    itrt = Item.ReceivedTime
    itbo = Item.Body
    Application.ActiveSheet.Cells(n, c) = itrt
    Application.ActiveSheet.Cells(n, c + 1) = itsj
    Application.ActiveSheet.Cells(n, c + 2) = itbo
    n = n + 1
 Next Item

我在Office 2010上。