【问题标题】:Sequential process Outlook rules顺序处理 Outlook 规则
【发布时间】:2023-03-25 06:55:01
【问题描述】:

我使用 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

结束子

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

【问题讨论】:

  • 查看您的代码可能会有所帮助:)
  • @brettdj 添加了方法大纲
  • 我认为您最好使用 ItemAdd 在电子邮件上运行您的代码。这样代码将按顺序运行。根据规则,将控制代码。
  • Sue Mosher 有一个有用的链接here 用于处理传入电子邮件的选项。简述NewMailEx eventItemAdd
  • 您可以实现自己的等待队列。这可以是按到达时间排序的集合。收到邮件后,您的规则处理程序会检查是否有等待的项目。如果有,则将新邮件放在队列的末尾。计时器事件处理程序用于处理等待项目。出于安全原因,我会将邮件存储在某个临时文件夹中以防止数据丢失。

标签: vba email outlook outlook-2010


【解决方案1】:

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

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

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

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

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

item.subject = maxtime item.save

【讨论】:

    【解决方案2】:

    GD 阿诺德,

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

    我使用类似的过程来自动上传收到的数据(作为电子邮件中的附件)并将其上传到 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
    

    这些步骤应该为您提供一个在新邮件到达时触发的 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-02
      • 2021-02-26
      • 1970-01-01
      • 2015-04-12
      • 1970-01-01
      • 2012-01-19
      相关资源
      最近更新 更多