Select 个收件箱在不同的 Outlook 帐户中

Select inboxes in different outlook accounts

如何select 我的二级 outlook 帐户的收件箱?下面的代码应该可以工作。但无论我选择帐户 1 还是 2(通过更改 outlookApp.Session.Accounts.Item(.) 行中的索引),它仍然会将 Fldr 设置为我的第一个帐户。我知道它这样做是因为消息框总是显示 'Searched 4 items',即使我的辅助电子邮件中有四个以上的项目。

Sub find_email2()

  Dim outlookApp
  Dim olNs As Outlook.Namespace
  Dim oAccount As Account
  Dim Fldr As Outlook.MAPIFolder
  Dim olMail As Variant
  Dim myTasks
  Dim sir() As String
  Dim attachmentFileName As String

  Set outlookApp = CreateObject("Outlook.Application")

  Set olNs = outlookApp.GetNamespace("MAPI")
  Set oAccount = outlookApp.Session.Accounts.Item(2) ''' makes no difference if this is 1 or 2

  Set Fldr = oAccount.Session.GetDefaultFolder(olFolderInbox)
  
  Set myTasks = Fldr.Items

  MsgBox "Searched " & myTasks.Count & " items"

End Sub

您获得相同的文件夹,因为 oAccount.Session 指向相同的 Namespace object,即使帐户不同也是如此。

您可以使用 Namespace.Stores collection 并调用 Store.GetDefaultFolder(olFolderInbox)(准备好处理错误,因为并非所有商店都公开相同的默认文件夹 - 例如 Public Folders store没有收件箱)或使用 Namespace.Accounts collection 并且每个 Account 使用 Account.DeliveryStore.GetDefaultFolder(olFolderInbox)