我构建了一个脚本,可以下载特定Outlook收件箱中的所有附件,但我需要对其进行修改,以便只抓取特定日期范围内的电子邮件。
这是我下载文件夹中所有附件的代码:
Sub DlAttachments()
MsgBox ("Cross fingers and click button to start download.")
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
strFolderName = "Special Inbox Admin"
Set objMailbox = objNamespace.Folders(strFolderName)
Set objFolder = objMailbox.Folders("Inbox")
Set colItems = objFolder.Items
For Each objMessage In colItems
intCount = objMessage.Attachments.Count
If intCount > 0 Then
For i = 1 To intCount
objMessage.Attachments.Item(i).SaveAsFile "C:\SP_INBOX_DUMP_temp\" & objFolder & "_" & _
objMessage.Attachments.Item(i).FileName
Next
End If
Next
MsgBox ("Download complete. Files downloaded to C:\SP_INBOX_DUMP_temp\")
End Sub
答案 0 :(得分:1)
使用限制方法将集合中的项目限制为特定日期范围内的项目:
Set colItems = objFolder.Items.Restrict("[ReceivedTime] > '06/01/2011 12:00' And [ReceivedTime] < '06/02/2011 12:00'")
基本上这样做会将结果集合中的电子邮件限制为6/1收到的电子邮件。这是空气代码,因此您可能需要使用语法。
有关限制方法的更多信息:http://msdn.microsoft.com/en-us/library/bb220369(v=office.12).aspx
答案 1 :(得分:0)
您可以使用对象消息的SentOn
属性。这会将您的代码更改为:
Dim myDate as Date
For Each objMessage In colItems
intCount = objMessage.Attachments.Count
'get the sent date
sentDate = objMessage.SentOn
'ref date (adapt format to your system date format dd/mm/yyyy or mm/dd/yyyy)
myDate = dateValue("31/1/2009")
If myDate > sentDate Then
'do whatever --> Exit For ?
End If
If intCount > 0 Then
For i = 1 To intCount
objMessage.Attachments.Item(i).SaveAsFile "C:\SP_INBOX_DUMP_temp\" & objFolder & "_" & _
objMessage.Attachments.Item(i).FileName
Next
End If
Next
此致
最高
答案 2 :(得分:0)
解决方法不是答案:由于无法成功实施Max的解决方案,我将日期范围内的电子邮件复制到单独的文件夹并修改了我的代码以对新文件夹执行操作。有点脏但是嘿,它有效。