【发布时间】:2015-08-17 16:11:42
【问题描述】:
我正在尝试执行以下操作:
- 搜索未读邮件
- 用特定关键字打开那些
- 从电子邮件中下载附件(如果我也可以过滤附件就好了)
- 将电子邮件标记为已读。
这就是我正在使用的。
Sub DownloadAttachmentFirstUnreadEmail()
Dim oOlAp As Object, oOlns As Object, oOlInb As Object
Dim oOlItm As Object, oOlAtch As Object
Dim strFilter As String
'~~> New File Name for the attachment
Dim NewFileName As String
NewFileName = AttachmentPath & Format(Date, "DD-MM-YYYY") & "-"
'~~> Get Outlook instance
Set oOlAp = GetObject(, "Outlook.application")
Set oOlns = oOlAp.GetNamespace("MAPI")
Set oOlInb = oOlns.GetDefaultFolder(olFolderInbox)
'~~> Check if there are any actual unread emails
If oOlInb.Items.Restrict("[UnRead] = True").count = 0 Then
MsgBox "NO Unread Email In Inbox"
Exit Sub
End If
'~~> Extract the attachment from the 1st unread email
For Each oOlItm In oOlInb.Items.Restrict("[UnRead] = True")
'The above loop begins to read everything that is unread.
'This is the part that gets tricky
'Here we need to begin filtering subject headline
'The line below defines what we are filtering
strFilter = "@SQL=" & Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " like '%sketch%'"
If filteredItems.count = 0 Then
Debug.Print "No emails found"
Found = False
Else
'~~> Check if the email actually has an attachment
If oOlItm.Attachments.count <> 0 Then
For Each oOlAtch In oOlItm.Attachments
'~~> Download the attachment
oOlAtch.SaveAsFile NewFileName & oOlAtch.FileName
Exit For
Next
Else
MsgBox "The First item doesn't have an attachment"
End If
End If
'~~> Mark 1st unread email as read
oOlItm.UnRead = False
DoEvents
oOlItm.Save
Exit For
Next
End Sub
【问题讨论】:
-
您定义的 strFilter 只是一个字符串...它实际上并没有过滤您的邮件结果..但是顺便说一句,您发布了您的代码,而不是您遇到的问题.. . 很难猜....
-
这里有什么问题?你的代码有问题吗?您的代码中缺少什么?
-
我还在努力,我的问题是让过滤器工作。