我找到了VBA代码(here),可以从Outlook文件夹中的所有电子邮件中下载附件。
我想从特定日期之后收到的电子邮件中下载附件。我尝试了Restrict
方法,但遇到以下错误:
运行时错误13:
类型不匹配
代码:
Dim msg As Outlook.MailItem
Dim fo As Outlook.Folder
Dim at As Outlook.Attachment
Dim cell1 As String
Dim sFilter As String
cell1 = Range("O2").Value 'Cell O2 has the following text in it: .xls
Set fo = Outlook.GetNamespace("MAPI").Folders("Winter").Folders("Inbox")
sFilter = "[Inbox.Items] > '" & Format("7/2/19 1:00am", "ddddd h:nn AMPM") & "'"
Set fo = fo.Items.Restrict(sFilter)
Dim lr As Integer
For Each msg In fo.Items 'Line where debugger points to
lr = Application.WorksheetFunction.CountA(sh.Range("A:A"))
sh.Range("A" & lr + 1).Value = msg.Subject
sh.Range("B" & lr + 1).Value = msg.Attachments.Count
For Each at In msg.Attachments
If VBA.InStr(at.Filename, cell1) > 0 Then
at.SaveAsFile sh.Range("F1").Value & "\" & lr & ". " & at.Filename
End If
Next
Next
MsgBox "Reports have been downloaded successfully"
End Sub
我尝试更改过滤器字符串,但没有成功。