收到新的Outlook消息时,无法运行VBA ItemAdd宏

问题描述

|| 因此,我研究了Outlook VBA ItemAdd事件,并创建了一个遵循该示例的类模块,但是由于某些原因,当我收到新消息时,宏不会触发。它是一个宏,用于解析电子邮件中的数据,然后将其保存到Excel文件中。宏的解析部分非常有用,因为我可以在自己的模块中运行它,而该模块可以手动运行,但是每次我收到特定消息的新副本时,我都试图使其附加新数据。我有一条规则,将这些电子邮件转发到特殊文件夹Folder X,在该文件夹中我希望代码搜索新消息。这是我的代码,位于类模块中。有什么想法为什么不能自动运行吗?谢谢!
Public WithEvents myOlItems As Outlook.Items


Public Sub Initialize_handler()

   \' Reference the items in the Inbox. Because myOlItems is declared
   \' \"WithEvents\" the ItemAdd event will fire below.
   Set myOlItems = Application.GetNamespace(\"MAPI\").GetDefaultFolder(olFolderInbox).Folders(\"Folder X\").Items


End Sub

Private Sub myOlItems_ItemAdd(ByVal Item As Object)


    Dim objMail As Outlook.MailItem
    Dim count As Integer
    Dim myTitlePos As Integer
    Dim myTitleLen As Integer
    Dim myVarPos As Integer
    Dim myVarLen As Integer
    Dim strPrice As String
    Dim strYear As String
    Dim myVarCRLF As Integer
    Dim myDate As Date
    Dim newLineTest As String


      \' Check to make sure it is an Outlook mail message,otherwise
      \' subsequent code will probably fail depending on what type
      \' of item it is.

      If TypeName(Item) = \"MailItem\" Then

      \'This is where all the working data parsing takes place.

      End If


End Sub
    

解决方法

只是这里的健全性检查...您是否创建了此类的实例? 即类似:
Dim c As MyClass
Private Sub Application_Startup()
    c = New MyClass
    \' If you don\'t rename Initialize_handler,you\'ll need:
    \' c.Initialize_handler
End Sub
在ThisOutlookSession中... 希望您也希望将
Initialize_handler
更改为
Class_Initialize
,除非您想对其进行明确的调用...     ,无需尝试在ThisOutlookSession中初始化例程,而是管理规则和警报以运行脚本,其中脚本将作为例程。 -Redplaya     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...