从特定的Outlook文件夹下载电子邮件并保存

我得到一个运行时错误5“无效的过程调用或参数”在下面的行

Set oOlInb = oOlns.GetDefaultFolder(olFolderInbox) 
  • 转到我的Outlook中标题为Spreadsheets的文件夹 – 将会有2-3个电子邮件。 可能会被读取,可能是未读 – 所以我不想像我在这里看到的一些代码一样限制为未读。
  • 从这些电子邮件下载所有的附件到我的桌面上的文件夹
  • 然后删除位于该Spreadsheets Outlook文件夹中的所有电子邮件(我还没有在代码中包含此部分)

我试过修改代码一堆。 但是我一直无法工作。 我想我在理解每个部分正在做什么时遇到了一些麻烦。

 Sub GetAttachments() Dim oOlAp As Object, oOlns As Object, oOlInb As Object Dim oOlItm As Object, SubFolder As Object, oOlAtch As Object Dim NewFileName As String Const AttachmentPath As String = "\\dsapc429pfs.pactual.net\homefolder02$\wellsty\Desktop\Testing Email Download" NewFileName = AttachmentPath & "Work?" Set oOlAp = GetObject(, "Outlook.Application") Set oOlns = oOlAp.getnamespace("MAPI") Set oOlInb = oOlns.GetDefaultFolder(olFolderInbox) Set SubFolder = oOlInb.Folders("Test") If SubFolder.oOlItm.Count > 0 Then For Each oOlAtch In oOlItm.attachments oOlAtch.SaveAsFile NewFileName & oOlAtch.FileName Exit For Next Else: End If End Sub 

首先,我build议从MSDN 中的Outlook 2010文章中的VBA入门开始 。

您需要更改每个循环,如下所示:

 '~~> Store the relevant info in the variables For Each oOlItm In SpecFolder.Items eSender = oOlItm.SenderEmailAddress dtRecvd = oOlItm.ReceivedTime dtSent = oOlItm.CreationTime sSubj = oOlItm.Subject sMsg = oOlItm.Body For Each att in oOlItm.Attachments att.SaveAsFile Environ("HOMEPATH") & "\My Documents\" & att.FileName Next oOlItm.Delete() Exit For Next 

您对SaveAsFile方法感兴趣, 它将附件保存到指定的path。

删除方法从包含该项目的文件夹中删除该项目。