将处理程序添加到 .ItemAdd Outlook 事件
Adding a Handler to .ItemAdd Outlook Event
我的代码:
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 的方法中,并尝试了来自不同 Whosebug 问题的解决方案,如下所示:Inbox.ItemAdd += New Outlook.ItemsEvents_ItemAddEventHandler(AddressOf Inbox_ItemAdd).
我没主意了。
问题是 Inbox_ItemAdd 即使我使用 AddHandler 也不会触发。
您应该通过将引发事件的对象存储在字段中来使其保持活动状态:
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
我的代码:
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 的方法中,并尝试了来自不同 Whosebug 问题的解决方案,如下所示:Inbox.ItemAdd += New Outlook.ItemsEvents_ItemAddEventHandler(AddressOf Inbox_ItemAdd).
我没主意了。
问题是 Inbox_ItemAdd 即使我使用 AddHandler 也不会触发。
您应该通过将引发事件的对象存储在字段中来使其保持活动状态:
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