VBA Outlook 新邮件

发布于 2024-11-08 13:24:01 字数 1155 浏览 2 评论 0原文

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

我向其中添加了一个简单的 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

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?

I added a simple MsgBox to 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 ThisOutlookSession module. Any adivice? Here is my code.

   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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

且行且努力 2024-11-15 13:24:01

尝试输入:

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

在“ThisOutlookSession”中

Try to put:

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

In "ThisOutlookSession"

唠甜嗑 2024-11-15 13:24:01

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文