使用 Access VBA 从 Outlook 获取附件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5332685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting attachment from outlook using Access VBA
提问by guest1
I have a created folder in my outlook named "Reports". This folder contains emails with one attachment in each email. I would like to use ACCESS VBA to save the attachments from the "Reports" folder in Outlook to a local drive in my computer. here is the code I have so far, but gives me errors. Please help:
我在 Outlook 中创建了一个名为“Reports”的文件夹。此文件夹包含电子邮件,每封电子邮件都带有一个附件。我想使用 ACCESS VBA 将 Outlook 中“报告”文件夹中的附件保存到我计算机的本地驱动器中。这是我到目前为止的代码,但给了我错误。请帮忙:
Sub GetAttachments()
Dim ns As NameSpace
Dim Inbox As Outlook.MAPIFolder
Dim folder As Outlook.MAPIFolder
Dim Item As Object
Dim Atmt As Attachment
Dim FileName As String
Dim i As Integer
Set ns = GetNamespace("MAPI")
Set Inbox = ns.Folders.Item("Reports") // I get an error in this line says an object could not be found
i = 0
If Inbox.Items.Count = 0 Then
MsgBox "There are no messages in the Inbox.", vbInformation, _
"Nothing Found"
Exit Sub
End If
For Each Item In Inbox.Items
For Each Atmt In Item.Attachments
FileName = "C:\Automation\" & Atmt.FileName
Atmt.SaveAsFile FileName // here is another error says method is not found
i = i + 1
Next Atmt
Next Item
回答by Todd
Is your Reports folder within your Inbox folder? You may need to do something like this:
您的报告文件夹是否在收件箱文件夹中?您可能需要执行以下操作:
Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
Set RptFolder = Inbox.Folders("Reports")
Your syntax for saving attachments looks correct (apart from your comments not being correct for VBA). You could print out the Filename that you are creating to see if it's a valid name. And I assume that you have created the Automation folder that you mention.
您保存附件的语法看起来正确(除了您的评论对 VBA 不正确)。您可以打印出您正在创建的文件名,看看它是否是一个有效的名称。我假设您已经创建了您提到的自动化文件夹。
Update:Try declaring your Atmt as an Outlook.Attachment. There is such a thing as an Access.Attachment which does not have a SaveAsFile method, and it's probably picking that one up first. If you include the library name, you should get the one you need.
更新:尝试将您的 Atmt 声明为 Outlook.Attachment。有一个 Access.Attachment 之类的东西,它没有 SaveAsFile 方法,它可能会先选择那个方法。如果您包含库名称,您应该得到您需要的名称。
Update 2:To get to your Reports folder, one way is to get the Inbox folder as you are currently doing, then get its parent, then get the Reports folder under that.
更新 2:要访问 Reports 文件夹,一种方法是像您当前所做的那样获取 Inbox 文件夹,然后获取其父文件夹,然后获取其下的 Reports 文件夹。
Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
Set Mailbox = Inbox.Parent
Set RptFolder = Mailbox.Folders("Reports")
Another way would be to scan the items under "ns" to find the one that starts with "Mailbox", then get the Reports folder under that. It seems a little more cumbersome than getting the parent of the inbox. That also seems cumbersome, but I couldn't find a way to get to the Mailbox folder directly.
另一种方法是扫描“ns”下的项目以找到以“Mailbox”开头的项目,然后获取其下的报告文件夹。这似乎比获取收件箱的父级要麻烦一些。这看起来也很麻烦,但我找不到直接进入邮箱文件夹的方法。
回答by Davis Rogers
Replace
代替
For Each Item In Inbox.Items
For Each Atmt In Item.Attachments
FileName = "C:\Automation\" & Atmt.FileName
Atmt.SaveAsFile FileName // here is another error says method is not found
i = i + 1
Next Atmt
With.....
和.....
For Each Item In Inbox.Items
For Each Atmt In Item.Attachments
FileName = "C:\Automation\" & Atmt.FileName
Attachments.SaveAsFile FileName // here is another error says method is not found
i = i + 1
Next Atmt
Outlook does not have a problem with atmt in the reference however, MS Access does. This should fix your problem.
Outlook 在参考中没有 atmt 问题,但是 MS Access 有问题。这应该可以解决您的问题。
Davis Rogers
戴维斯·罗杰斯
回答by Diogo Neves
Replace
代替
Dim Atmt As Attachment
with
和
Dim Atmt As Outlook.Attachment
It'll make Access find the correct Class for atmt.
它将使 Access 为 atmt 找到正确的类。