对多个电子邮件执行一次批处理脚本

时间:2015-12-04 21:03:04

标签: vba loops email outlook-vba

我有多封电子邮件进来(每天我收到3封电子邮件,订购3类别的订单)。电子邮件主题的格式为:

" ORDERS EXTRACT - [类别] - [日期] "。

[类别] 可以是Category 1Category 2Category 3 [日期] 是以DD / MM / YYYY格式发送电子邮件的日期。

我有一个规则设置来搜索' 订单'然后调用以下代码。

我希望在保存所有电子邮件附件后运行Complete.bat,我只想调用一次。

我尝试通过创建另一个名为saveAttachtoDisk_CATEGORY1(itm)的子集来尝试这样做,只有在找到" 类别1 "在这个主题。然后它会保存附件,但也会在主题中搜索类别1并搜索昨天的日期。

我想要一个不依赖于日期的更好的解决方案。全局变量可以在我将变量设置为1的情况下工作,然后发送运行Complete.bat,然后在变量= 1的情况下运行Complete.bat。不确定放置此变量的位置(全局变量?)因为两个子模块似乎都放错了并引用它。

这两个模块都保存在'模块' Microsoft Outlook VBA的一部分。

Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
    Dim objAtt As Outlook.Attachment
    Dim SaveFolder As String
    SaveFolder = "D:\Orders\"
    For Each objAtt In itm.Attachments
         objAtt.SaveAsFile SaveFolder & "\" & objAtt.DisplayName
         objAtt.Delete
    Next
    itm.Save
End Sub

其他模块:

Public Sub saveAttachtoDisk_CATEGORY1(itm As Outlook.MailItem)
    Dim objAtt As Outlook.Attachment
    Dim SaveFolder As String
    SaveFolder = "D:\Orders\"
    For Each objAtt In itm.Attachments
        objAtt.SaveAsFile SaveFolder & "\" & objAtt.DisplayName
        objAtt.Delete
    Next
    itm.Save
    If InStr(1, itm.Subject, "ORDERS EXTRACT - Category 1 -" & Format(Date, "dd/mm/yyyy")) Then
        Shell "D:\Orders\Complete.bat"
    End If
End Sub

1 个答案:

答案 0 :(得分:3)

<强>假设

  • OP每天会收到三封电子邮件(尽管如此) 可在代码中自定义)
  • 受试者将始终以“ORDERS EXTRACT - ”开头,而不是其他人 电子邮件将以该代码开头
  • OP希望在收到后每天运行一次Complete.bat 第三个ORDERS EXTRACT电子邮件。
  • OP isady设置了规则,以便在收到后运行SaveAttachtoDisk 订单提取电子邮件。此规则可以更改为运行 CategorySaveAndComplete
  • OP正在使用Outlook 2013或更高版本

提议的解决方案

以下代码将保存每个Orders Extract电子邮件的附件,然后检查是否已收到所有这三个。我选择不使用.Find和.FindNext,因为这些方法不能使用通配符,因此需要对类别名称进行硬编码。我还选择不使用.Restrict,因为我们正在搜索的只有三个项目。

也就是说,.Find和.Restrict的解决方案也是有效的,并且在某些条件下会比下面的方法更好,例如在收件箱中一直有很多项目的用户。

请注意,订单提取电子邮件的预期数量,要匹配的主题字符串以及要检查的先前日期都可以通过常量设置。我实施了之前的日期检查,以防OP想要在前一天检查。

Option Explicit

Public Const C_ExpectedOrderCount As Integer = 3 'Set number of expected emails for categories
Public Const C_SubjectFormat As String = "ORDERS EXTRACT - *"
Public Const C_PrevDatesToCheck As Integer = 0 'If the Outlook app may not be open every day, set this to the number of prior days the script should also check.

Public Sub CategorySaveAndComplete(itm As Outlook.MailItem)

    'Do not take any action if this is not an ORDERS EXTRACT email.
    If itm.Subject Like C_SubjectFormat Then

        Dim objAtt As Outlook.Attachment
        Dim SaveFolder As String
        SaveFolder = "D:\Orders\"
        For Each objAtt In itm.Attachments
            objAtt.SaveAsFile SaveFolder & "\" & objAtt.DisplayName
            objAtt.Delete
        Next
        itm.Save

        'Check all emails in Inbox for ORDERS EXTRACT - * - DATE
        Dim Item As Object
        Dim objNS As Outlook.NameSpace
        Set objNS = GetNamespace("MAPI")
        Dim olFolder As Outlook.MAPIFolder
        Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
        Dim iLoop As Integer
        Dim iCount As Integer
        Dim DateCheck As Date

        For iLoop = 0 To C_PrevDatesToCheck
            'Reset DateCheck and iCount if we are looping through days
            DateCheck = DateSerial(Year(Date), Month(Date), Day(Date)) - iLoop
            iCount = 0
            'Loop through mail items
            For Each Item In olFolder.Items
                If Item.Class = 43 Then
                    'This is an email. Check if it matches our criteria.
                    If Item.Subject Like C_SubjectFormat And CDate(CLng(Item.ReceivedTime)) = DateCheck Then iCount = iCount + 1
                End If
            Next
            'If we have met the expected targets, then run the batch file.
            If iCount = C_ExpectedOrderCount Then
                'We have exactly the expected number of items. Run the batch file.
                Shell "D:\Orders\Complete.bat"
            ElseIf iCount > C_ExpectedOrderCount Then
                'More items than expected. Check if user is OK with running batch file; if so, run it now.
                If MsgBox("More order extracts than expected were received. Expected " & _
                C_ExpectedOrderCount & "; received " & iCount & " for " & Format(DateCheck, "mmm d, yy") & _
                ". Would you like to run the Complete.bat file now?", vbYesNo) = vbYes Then Shell "D:\Orders\Complete.bat"
            End If
        Next iLoop
    End If
End Sub