收听特定的Outlook帐户与vb

  • 本文关键字:vb Outlook outlook-addin
  • 更新时间 :
  • 英文 :


我想在这里请求您的帮助。我正在尝试编写脚本(Outlook 加载项 VSTO(来侦听来自特定 Outlook 帐户的所有传入电子邮件。在我的Outlook应用程序中,我已经设置了几个帐户(交换帐户(,但我只对其中一个感兴趣。我有以下代码,它侦听当前默认帐户中的收件箱文件夹。

Private Sub ThisAddIn_Startup() Handles Me.Startup
Dim outlookNameSpace As Outlook.NameSpace
outlookNameSpace = Me.Application.GetNamespace("MAPI")
inbox = OutlookNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
Mailitem = inbox.Items
End Sub
Private Sub Items_ItemAdd(ByVal item As Object) Handles Mailitem.ItemAdd
If TypeOf (item) Is Outlook.MailItem Then
--Do some things here--
End if
End Sub

代码运行完美,但它正在侦听默认帐户。我想更改它以收听在Outlook中设置的另一个帐户。

你有什么想法吗?

谢谢!!

不使用Namespace.GetDefaultFolder,在Namespace.Stores集合中找到您要查找的存储,并使用Store.GetDefaultFolder从该存储中检索收件箱文件夹

这可能会有所帮助!! 在 Outlook 会话中复制此代码。 展望会话图像

Private Sub Application_Startup()
Dim objNS As NameSpace
Set objNS = Application.Session
Set olInboxItems = GetFolderPath("your other email address nameInbox").Items
Set objNS = Nothing
Set objInbox = Application.Session.GetDefaultFolder(olFolderInbox)
Set objItems = objInbox.Items
End Sub
Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
On Error Resume Next
'your code what you want to do  with additional email address
End Sub
Private Sub objItems_ItemAdd(ByVal Item As Object)
'your code for your default email address
End Sub
Function GetFolderPath(ByVal FolderPath As String) As Outlook.Folder
Dim oFolder As Outlook.Folder
Dim FoldersArray As Variant
Dim i As Integer
On Error GoTo GetFolderPath_Error
If Left(FolderPath, 2) = "\" Then
FolderPath = Right(FolderPath, Len(FolderPath) - 2)
End If
'Convert folderpath to array
FoldersArray = Split(FolderPath, "")
Set oFolder = Application.Session.Folders.Item(FoldersArray(0))
If Not oFolder Is Nothing Then
For i = 1 To UBound(FoldersArray, 1)
Dim SubFolders As Outlook.Folders
Set SubFolders = oFolder.Folders
Set oFolder = SubFolders.Item(FoldersArray(i))
If oFolder Is Nothing Then
Set GetFolderPath = Nothing
End If
Next
End If
'Return the oFolder
Set GetFolderPath = oFolder
Exit Function
GetFolderPath_Error:
Set GetFolderPath = Nothing
Exit Function
End Function

最新更新