如何获得仅在最新电子邮件上运行脚本的Outlook规则?

时间:2018-12-18 22:25:08

标签: vba outlook outlook-vba

我写了一个Outlook脚本来提取附件并将​​其保存到我的服务器。我还在Outlook中创建了一条规则,仅将此脚本应用于具有特定主题行的电子邮件。

但是,这些电子邮件是主题相同的每日报告。每当我尝试运行此规则时,它都会从给定的日期起获取带有该主题行的任何报告,并保存该附件,而不是最新的报告。

是否可以在Outlook中甚至在我使用的脚本中解决此问题?我很高兴在此问题上能获得的所有清晰度!

Public Sub SaveAttachmentsToDisk4(MItem As Outlook.MailItem)
Dim oAttachment As Outlook.Attachment
Dim sSaveFolder As String
sSaveFolder = "K:\BI and Information\Farah\"
For Each oAttachment In MItem.Attachments
oAttachment.SaveAsFile sSaveFolder & oAttachment.DisplayName
Next
End Sub

1 个答案:

答案 0 :(得分:0)

您可以获取收件箱中的未读电子邮件附件,并将未读电子邮件设置为在保存附件时阅读。

请参考以下代码:

Sub SaveAutoAttach(item As Outlook.MailItem)

Dim object_attachment As Outlook.attachment

Dim saveFolder As String
Dim oOutlook As Object
Dim oOlns As Object
Dim oOlInb As Object
Dim unRead, m As Object, att As Object
Dim some As String, other As String

Const olFolderInbox = 6

'~~> Get Outlook instance
Set oOutlook = GetObject(, "Outlook.application")
Set oOlns = oOutlook.GetNamespace("MAPI")
Set oOlInb = oOlns.GetDefaultFolder(olFolderInbox)

'~~> Check if there are any actual unread emails
Set unRead = oOlInb.Items.Restrict("[UnRead] = True")

If unRead.Count = 0 Then
    MsgBox "NO Unread Email In Inbox"
Else

    some = ""
    other = ""
    saveFolder = "D:\"
    For Each m In unRead
        If m.Attachments.Count > 0 Then
            For Each object_attachment In m.Attachments
            ' Criteria to save .doc files only
                If InStr(object_attachment.DisplayName, ".doc") Then
                    object_attachment.SaveAsFile saveFolder & "\" & object_attachment.DisplayName
                End If
             Next
        End If
        m.Delete
    Next m
End Sub