1

我的代码:

Imports Microsoft.Office.Interop
Imports OpenQA.Selenium
Imports OpenQA.Selenium.Chrome
Imports System.Text
Imports System.Windows.Threading
Imports UiPath
Imports UiPath.Orchestrator.Extensibility
Imports System.IO
Imports System.Net.NetworkInformation
Imports EASendMail
Imports VB = Microsoft.VisualBasic

Class MainWindow


Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)


    'Dim OutlookApp As New Outlook.Application
    Dim OutlookApp As Outlook.Application = GetObject(, "Outlook.Application")
    Dim Inbox = OutlookApp.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox).Items

    AddHandler Inbox.ItemAdd, AddressOf Inbox_ItemAdd
    'Inbox.ItemAdd += New Outlook.ItemsEvents_ItemAddEventHandler(AddressOf Inbox_ItemAdd)
    'This line does not work, but it works in the C# stack question for some reason

End Sub

Public Sub Inbox_ItemAdd(ByVal Item As Object)
    Dim mail As Outlook.MailItem = CType(Item, Outlook.MailItem)

    If Item IsNot Nothing Then
        MsgBox("Received")
    End If
End Sub

End Class

我尝试将 .ItemAdd 中的处理程序添加到名为 Inbox_ItemAdd 的方法中,并尝试了来自不同 stackoverflow 问题的解决方案,例如:Inbox.ItemAdd += New Outlook.ItemsEvents_ItemAddEventHandler(AddressOf Inbox_ItemAdd)

我没主意了。

问题是即使我使用 AddHandler 也不会触发 Inbox_ItemAdd。

4

1 回答 1

3

您应该通过将引发事件的对象存储在字段中来保持该对象处于活动状态:

Class MainWindow

    Dim Inbox As Outlook.Items

    Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)

        ...
        Inbox = OutlookApp.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox).Items

        AddHandler Inbox.ItemAdd, AddressOf Inbox_ItemAdd

    End Sub

    Public Sub Inbox_ItemAdd(ByVal Item As Object)
        ...
    End Sub

End Class
于 2022-01-17T18:23:52.057 回答