Vba Outlook触发器随机停止工作

Vba Outlook触发器随机停止工作,vba,outlook,triggers,ms-office,Vba,Outlook,Triggers,Ms Office,每天早上我们都会收到一封自动邮件,上面有今天的价格。我在outlook会话中设置了一个触发器,用于检查每封新电子邮件,如果它有主题,则启动一个脚本。问题是,这种触发器有时才起作用。我一直让机器运行,outlook打开,但我发现触发器在一段时间后停止工作。通常我只需要重新启动outlook,有时我只需要修改并重新保存代码 有什么我可以改变的,使这个触发器更可靠吗 注意:我没有访问任务计划程序的权限 更新:我已开始收到以下警报: 文件C:\Users\me\AppData\Local\Microso

每天早上我们都会收到一封自动邮件,上面有今天的价格。我在outlook会话中设置了一个触发器,用于检查每封新电子邮件,如果它有主题,则启动一个脚本。问题是,这种触发器有时才起作用。我一直让机器运行,outlook打开,但我发现触发器在一段时间后停止工作。通常我只需要重新启动outlook,有时我只需要修改并重新保存代码

有什么我可以改变的,使这个触发器更可靠吗

注意:我没有访问任务计划程序的权限

更新:我已开始收到以下警报:

文件C:\Users\me\AppData\Local\Microsoft\Outlook\Me@host.com.ost 正在使用中,无法访问。关闭正在使用的任何应用程序 请删除此文件,然后重试。您可能需要重新启动计算机

点击OK不会中断脚本的运行,但它是否会暂停触发器的运行

以下是脚本:

'Placed in Microsoft Outlook Objects > ThisOutlookSession

Private WithEvents olInboxItems As Items

Private Sub Application_Startup()
    Set olInboxItems = Session.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
    If TypeOf Item Is mailItem Then
        Call handleMessage(Item.EntryID)
    End If
End Sub

Public Function handleMessage(strMailItemID As String)
    ' This function takes an email ID and determines whether it's our pricing sheet email.

    Dim mailItem As Outlook.mailItem
    Dim path As String, targetSubj As String
    Dim result As Boolean
    result = False
    targetSubj = "Today's Pricing"
    Set mailItem = Application.Session.GetItemFromID(strMailItemID)

    Debug.Print (mailItem.Subject) 'I use this to test if my script is working
                                   'I find that it logs the subject and then simply cuts off. 

    If (Mid(mailItem.Subject, 1, Len(targetSubj)) = targetSubj) Then
        Debug.Print ("Grabbed Target " & Date)
        path = "C:\pathToPricing\Pricing.xls"
        mailItem.Attachments.Item(1).SaveAsFile path
        Call runExcelMacro("C:\pathToPricing\pricingAuto.xlsm", "processPricing")
        result = True

    End If

    handleMessage = result
End Function
当一次向文件夹中添加大量项目(超过16项)时,不会触发该事件-这是Outlook中的一个已知问题

取而代之的是,可以考虑在Outlook中创建规则,然后分配一个VBA宏子来运行。它应该如下所示:

public sub test(mail as MailItem)
   ' do whatever you need
end sub
另一个解决方案是使用计时器检查新电子邮件

此外,您可能会发现以下系列文章很有帮助:


    • 如果没有解决方案,您可以在记起或注意到触发器损坏时,通过按钮运行应用程序启动

      删除Private,这样您就有了它

      Sub Application_Startup()
          Set olInboxItems = Session.GetDefaultFolder(olFolderInbox).Items
      End Sub
      
      将应用程序启动置于快速访问工具栏或功能区中的按钮上


      如果你愿意的话,你可以把Private放回去。按钮应该还能用。

      最初我用的是NewMailEx,但我发现它更不可靠。我将研究你提出的其他解决方案。谢谢