【发布时间】:2018-07-27 14:34:10
【问题描述】:
我正在尝试根据我有限的知识修改现有代码以满足我的需求。我已经根据 Dmitry 的建议自定义了代码,这导致将附件保存到计算机中的目标文件夹中。但是使用 startdate 和 enddate 字符串的变量日期低于错误
运行时错误'-2147352567(80020009)':
无法解析条件。 “SQL="urn:schemas:httpmail:datereceived"..." 处出错。
错误发生在 Set myRestrictItems = myItems.Restrict(Filter)
但它适用于直接日期
Filter = "@SQL=" & Chr(34) & "urn:schemas:httpmail:datereceived" & _ Chr(34) & " >= '01/01/2017' 和 " & _ Chr(34) & "urn:schemas:httpmail:datereceived" & _ Chr(34) & "
Sub Extract()
Dim valid As Boolean: valid = True
Dim oShell As Object
Dim Filter As String
Dim myNamespace As Outlook.NameSpace
Dim myRestrictItems As Outlook.Items
Dim myItems As Outlook.Items
Dim myItem As Object
Dim i As Long
Dim oAttachment As Outlook.Attachment
Dim StartDate, EndDate As String
Set myNamespace = Application.GetNamespace("MAPI")
Set myFolder = myNamespace.PickFolder
Set myItems = myFolder.Items
StartDate = InputBox("Enter the Start Date in dd/mm/yyyy format", vbOKOnly)
EndDate = InputBox("Enter the End Date in dd/mm/yyyy format", vbOKOnly)
Filter = "@SQL=" & Chr(34) & "urn:schemas:httpmail:datereceived" & _
Chr(34) & " >= " & StartDate & _
Chr(34) & "urn:schemas:httpmail:datereceived" & _
Chr(34) & " <= " & EndDate & ""
Set myRestrictItems = myItems.Restrict(Filter)
strFolderpath = CreateObject("WScript.Shell").SpecialFolders(16)
strFolderpath = strFolderpath & "\Attachments\"
For i = myRestrictItems.Count To 1 Step -1
Set myItem = myRestrictItems(i)
For Each oAttachment In myItem.Attachments
oAttachment.SaveAsFile strFolderpath & oAttachment.FileName
Next
Next
End Sub
【问题讨论】:
-
在代码块中添加源代码