VBA 展望新邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6084442/
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:16:36  来源:igfitidea点击:

VBA outlook new mail

vbaoutlookoutlook-vba

提问by loveforvdubs

I am trying to run a function every time a new mail arrives in outlook. I have been doing some searching but I am unable to find I way to fire code every time an email arrives. Is there a new mail event that I could utilize?

每次新邮件到达 Outlook 时,我都试图运行一个函数。我一直在做一些搜索,但每次收到电子邮件时我都找不到触发代码的方法。是否有我可以使用的新邮件事件?

I added a simple MsgBoxto it to be able to see if the event is firing but it did not seem to be working. I placed this code in the ThisOutlookSessionmodule. Any adivice? Here is my code.

MsgBox向它添加了一个简单的东西,以便能够查看事件是否正在触发,但它似乎没有工作。我将此代码放在ThisOutlookSession模块中。有什么建议吗?这是我的代码。

   Public WithEvents myOlApp As Outlook.Application

    Sub Initialize_handler()
        Set myOlApp = CreateObject("Outlook.Application")
    End Sub

    Private Sub myOlApp_NewMail()
        Dim myExplorers As Outlook.Explorers
        Dim myFolder As Outlook.MAPIFolder
        Dim x As Integer
        Set myExplorers = myOlApp.Explorers
        Set myFolder = myOlApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
        If myExplorers.Count <> 0 Then
            For x = 1 To myExplorers.Count
                On Error GoTo skipif
                If myExplorers.Item(x).CurrentFolder.Name = "Inbox" Then
                    MsgBox ("Test")
                    myExplorers.Item(x).Display
                    myExplorers.Item(x).Activate
                    Exit Sub
                End If
    skipif:
            Next x
         End If
         On Error GoTo 0
         myFolder.Display
    End Sub

回答by Matt Immer

Try to put:

尝试放置:

Private Sub Application_NewMail()
    MsgBox "New mail"
End Sub

In "ThisOutlookSession"

在“ThisOutlookSession”中

回答by jonsca

There's a good example on MSDNshowing how to display the inbox when a new mail arrives (using Outlook.Explorers). You can probably adapt it pretty readily for your own program.

MSDN上有一个很好的例子,展示了新邮件到达时如何显示收件箱(使用Outlook.Explorers)。您可能可以很容易地为您自己的程序调整它。