Tik*_*aty 11 email vba loops outlook-vba
我有多封电子邮件进来(每天我收到3封电子邮件,订购3类别的订单).电子邮件主题的格式如下:
" 订单提取 - [类别] - [日期] ".
其中【类别】可以Category 1,Category 2或Category 3.[日期]是以DD/MM/YYYY格式发送电子邮件的日期.
我有一个规则设置来搜索' Orders '然后调用下面的代码.
我希望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
Run Code Online (Sandbox Code Playgroud)
其他模块:
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
Run Code Online (Sandbox Code Playgroud)
假设
建议的解决方案
下面的代码将保存每封订单提取电子邮件的附件,然后检查是否已收到所有三个附件。我选择不使用 .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
Run Code Online (Sandbox Code Playgroud)