当我收到新的 Outlook 邮件时,无法运行 VBA ItemAdd 宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6126668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:19:14  来源:igfitidea点击:

Can't get VBA ItemAdd macro to run when I receive a new Outlook message

vbaoutlook

提问by SeanJean

So i researched the Outlook VBA ItemAdd event and created a class module that follows the example, but for some reason my macro won't fire when I receive new messages. It is a macro that parses data within the email and then saves it to an excel file. The parsing portion of the macro works great as I have it working in it's own module that I can run manually, but I was trying to also have it append new data each time I receive a new copy of the specific message. I have a rule that forwards these emails into a special folder, Folder X, where I want the code to search for new messages. Here is my code, which is in a class module. Any ideas why this isn't running automatically? Thanks!

因此,我研究了 Outlook VBA ItemAdd 事件并创建了一个遵循示例的类模块,但由于某种原因,当我收到新邮件时,我的宏不会触发。它是一个宏,用于解析电子邮件中的数据,然后将其保存到 Excel 文件中。宏的解析部分工作得很好,因为我在它自己的模块中工作,我可以手动运行,但我试图在每次收到特定消息的新副本时附加新数据。我有一个规则将这些电子邮件转发到一个特殊的文件夹,文件夹 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

采纳答案by Simon Cowen

Just a sanity check here... have you created an instance of this class?

只是在这里检查一下……您是否创建了此类的实例?

i.e. something like:

即类似:

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

in ThisOutlookSession...

在ThisOutlookSession中...

I expect you'll also be wanting to change Initialize_handlerto Class_Initializeunless you want to have to make an explicit call to it...

我希望您也想更改Initialize_handler为,Class_Initialize除非您想对其进行显式调用...

回答by red

Instead of trying to have the routine initialize in ThisOutlookSession, Manage Rules & Alerts to Run a Script, where your script will be your routine. -Redplaya

与其尝试在 ThisOutlookSession 中初始化例程,不如管理规则和警报以运行脚本,您的脚本将成为您的例程。-Redplaya