1

我使用 Outlook 规则通过 VBA 宏处理传入的邮件。在 vba 中,会触发各种操作来处理传入邮件的附件。

问题是,有时会有一堆电子邮件需要处理。我似乎无法找到一种方法来逐个触发。如果有堆栈,我想在处理下一封邮件之前等待几秒钟。在宏中放置睡眠方法似乎没有效果。该规则似乎没有等待上一条消息完成。

我的方法是这样的:

有没有办法完成这种行为?

Private Sub ProcessMail(ByVal Item As Object)
Dim objNS As Outlook.NameSpace
Set objNS = GetNamespace("MAPI")

If TypeOf Item Is Outlook.MailItem Then
    Dim Msg As Outlook.MailItem

        DoProcessingMethod 

    End If
End If

结束子

在方法中放置等待或睡眠不会导致它被一一处理。

4

2 回答 2

1

GD阿诺德,

您确实可以ItemAdd按照@Brett 的回答使用该选项。

我使用类似的过程来自动上传收到的数据(作为电子邮件中的附件)并将其上传到 MySQL 数据库。该操作由 ItemAdd 方法触发,并逐一检查邮件。

简化说明:

在您的 VBA 代码中添加一个名为“EventClassModule”的类

在您的班级中,键入

Public WithEvents dItems As Outlook.Items

在您的 ThisOutlookSession 中创建一个注册 event_handler 的子程序:

Sub Register_Event_Handler()

    Set myClass.dItems = Outlook.Items

End Sub

在您的 ThisOutlookSession 中创建一个处理 ItemAdd 事件的子程序,如下所示:

Private Sub dItems_ItemAdd(ByVal newItem As Object)

  On Error GoTo ErrorHandler
  Dim msg As Outlook.MailItem
  If newItem.Class = olMail Then
    Set msg = newItem
    'Do something with the msg item, check rules, check subject, check whatever
    'This will process messages when the arrive in your mailbox one by one.


  End If
ProgramExit:
  Exit Sub
ErrorHandler:
  MsgBox Err.Number & " - " & Err.Description
  Resume ProgramExit
End Sub

这些步骤应该为您提供一个在新邮件到达时触发的子程序。

然后,您可以调用如下所示的函数/子。下面的子程序基于可选ruleSet变量运行所有规则,它检查规则rule.Name集,如果ruleSet字符串存在,rule.Name则执行一些代码。这样,您可以拥有多个规则,并且仅根据它们所属的“规则集”执行其中一些规则。您可以通过更改其名称来定义它。

这是 Outlook 中“运行规则”选项的改进。

其中一些代码来自这里:Setting VBA to read personal inbox

Sub runRules(Optional ruleSet As String)
    Dim olStore As Outlook.Store
    Dim myRules As Outlook.Rules
    Dim tmpInbox As Outlook.Folder
    Dim tmpSent As Outlook.Folder

    Dim rl As Outlook.Rule
    'On Error Resume Next
    'toTmpBox (ruleSet)

    ' get default store (where rules live)
    Set olStore = Application.Session.DefaultStore

    With olStore
        Set tmpInbox = .GetDefaultFolder(olFolderInbox) '.Folders("tmpInbox")
        Set tmpSent = .GetDefaultFolder(olFolderSentMail) '.Folders("tmpSentBox")
    End With

    ' get rules
    Set myRules = olStore.GetRules

    ' iterate through all the rules
    For Each rl In myRules
            Debug.Print rl.Conditions.Body.Enabled & " " & rl.Conditions.Body.Text

            If InStr(LCase(rl.Name), ruleSet) > 0 And (rl.Enabled) Then

                rl.Execute ShowProgress:=True, Folder:=tmpInbox

                If ruleSet = "autorun" Then
                    rl.Execute ShowProgress:=True, Folder:=olStore.GetDefaultFolder(olFolderSentMail)

                End If

                ruleList = ruleList & vbCrLf & rl.Name
            End If
    Next


    ' tell the user what you did
    ruleList = "These rules were executed " & _
          vbCrLf & ruleList
    MsgBox ruleList, vbInformation, "Macro: RunMyRules"

CleanUp:

    Set olStore = Nothing
    Set tmpInbox = Nothing
    Set tmpSent = Nothing

    Set rl = Nothing
    Set myRules = Nothing


End Sub
于 2015-10-16T03:59:32.010 回答
0

我遇到了类似的问题。在我的情况下,我的工具会以固定的时间间隔运行,并且每次我只需要捕获新电子邮件。现在新电子邮件可能是一封或多封。我找到的解决方案如下所示。

每次该工具都会运行。它将捕获新电子邮件并标记一个简单的“,”或“|” 在主题结尾处您选择的任何内容,以使任何人都不会注意到。现在,下次运行该工具时,它会检查一两天收到的电子邮件(根据您的要求)是否具有这些标记。

如果电子邮件通信是一种方式,则此解决方案有效。如果我们将这些电子邮件用于连锁电子邮件,那么它们是另一种解决方案。

在这里,您必须保存上次运行中捕获的电子邮件的最大时间。现在,每次运行时,您只需运行一整天,并放置一个应该大于上次捕获时间的 if 语句。

现在要存储您可能需要创建文件夹和电子邮件的最长时间。电子邮件可以帮助您存储每次运行发生的时间

item.subject = maxtime item.save

于 2014-06-17T02:22:42.307 回答