我更改了一些代码,用于将选定的消息附件获取到我的硬盘上,如下所示:
Public Sub SaveAttachments()
Dim objOL As Outlook.Application
Dim objMsg As Outlook.MailItem 'Object
Dim objAttachments As Outlook.Attachments
Dim objSelection As Outlook.Selection
Dim I As Long
Dim lngCount As Long
Dim strFile As String
Dim strFolderpath As String
Dim strDeletedFiles As String
Dim Counter As Long
strFolderpath = "D:\attachments"
If (Dir$(strFolderpath, vbDirectory) = "") Then
MsgBox "'" & strFolderpath & "' not exist"
MkDir strFolderpath
MsgBox "'" & strFolderpath & "' we create it"
Else
MsgBox "'" & strFolderpath & "' exist"
End If
' Get the path to your My Documents folder
'strFolderpath = CreateObject("WScript.Shell").SpecialFolders(16)
strFolderpath = strFolderpath & "\"
On Error Resume Next
' Instantiate an Outlook Application object.
Set objOL = CreateObject("Outlook.Application")
' Get the collection of selected objects.
Set objSelection = objOL.ActiveExplorer.Selection
' The attachment folder needs to exist
' You can change this to another folder name of your choice
' Set the Attachment folder.
strFolderpath = strFolderpath
' Check each selected item for attachments.
Counter = 1
For Each objMsg In objSelection
Set objAttachments = objMsg.Attachments
lngCount = objAttachments.Count
If lngCount > 0 Then
' Use a count down loop for removing items
' from a collection. Otherwise, the loop counter gets
' confused and only every other item is removed.
For I = lngCount To 1 Step -1
' Get the file name.
strFile = objAttachments.Item(I).FileName
' Combine with the path to the Temp folder.
strFile = strFolderpath & Counter & "_" & strFile
' Save the attachment as a file.
objAttachments.Item(I).SaveAsFile strFile
Counter = Counter + 1
Next I
End If
Next
ExitSub:
Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
MsgBox "All Selected Attachments Have Been Downloaded ..."
End Sub
我的目标电子邮件使用 imap 服务...
这个 vb 代码运行完美!
但我的问题是,下载完成后,附件文件夹中没有我们需要的文件!
我有 450未读我的收件箱里的所有电子邮件都带有附件...
但是附件文件夹中只有 200 个文件!(由上层代码创建)
我该如何解决这个问题?
这个问题似乎与未读消息和我的 ADSL 速度有关(但它不应该,我不知道?!)
当你阅读一封电子邮件时,似乎 Outlook 会对该电子邮件进行一些处理,因此下次由于它的缓存,该电子邮件运行得更快。
我该如何处理带有上层代码的未读电子邮件?
或者对这个问题有什么想法吗?
最后,我将非常感谢您的审查,并添加或更正我的代码
答案1
如果任何附件具有相同的名称,它们可能会被覆盖(不记得是否.SaveAsFile
会覆盖或导致错误),因此您可能需要先检查文件名是否存在,或者在文件名中添加其他标识符(可能是消息主题?)。
可以将 Outlook 设置为对使用 IMAP 的电子邮件帐户执行不同的操作,例如仅下载标题,仅在打开时获取整个邮件,或者首先下载整个邮件。
您能否进行一些随机检查,看看已读邮件的附件是否全部存在,而未读邮件的附件是否不存在?这将证实 IMAP 理论,并且如果尚未修复,也许可以在代码中通过下载选定邮件的方法进行修复。