vba 从 Outlook 获取收件箱

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6849068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:42:40  来源:igfitidea点击:

Get Inboxes from Outlook

vbaoutlookoutlook-addinoutlook-vbaoutlook-object-model

提问by Ivan G.

I configured two Exchange accounts in Outlook 2010, however I cant find out how to get to Inbox of the second account. Session.GetDefaultFolder()always return the first one.

我在 Outlook 2010 中配置了两个 Exchange 帐户,但是我无法找到如何进入第二个帐户的收件箱。Session.GetDefaultFolder()总是返回第一个。

Even enumerating Session.Accounts, finding the right account and calling Session.Account(found one).Store.GetDefaultFolder()returns wrong Inbox (from the default exchange account, not the secondary).

即使枚举 Session.Accounts,找到正确的帐户并调用也会Session.Account(found one).Store.GetDefaultFolder()返回错误的收件箱(来自默认的交换帐户,而不是辅助帐户)。

回答by JimmyPena

Does this show you all the available Inboxes?

这是否会向您显示所有可用的收件箱?

Sub LoopThroughInboxes

Dim ol As Outlook.Application
Dim ns As Outlook.NameSpace
Dim i As Long

Set ol = Outlook.Application
Set ns = ol.GetNamespace("MAPI")

For i = 1 To ns.Folders.Count
 Debug.Print ns.Folders(i).Name
Next i

If so then ns.Folders(i).Folders("Inbox")will get you the Inbox for each mailbox.

如果是这样,那么ns.Folders(i).Folders("Inbox")将为您提供每个邮箱的收件箱。

回答by Dmitry Streblechenko

Use Store.GetDefaultFolder instead of Namespace.GetDefaultFolder. Note that Store.GetDefaultFolder was added in Outlook 2010. In the earlier versions of Outlook use Extended MAPI (C++ or Delphi) or Redemption(RDOStore.GetDefaultFolder.

使用 Store.GetDefaultFolder 而不是 Namespace.GetDefaultFolder。请注意,在 Outlook 2010 中添加了 Store.GetDefaultFolder。在 Outlook 的早期版本中,使用扩展 MAPI(C++ 或 Delphi)或Redemption(RDOStore.GetDefaultFolder。

回答by Vijay Kumbhani

To Go to Mapix library

前往 Mapix 图书馆

Mapix library link as given below

Mapix 库链接如下所示

Mapix library for C++/MFC

用于 C++/MFC 的 Mapix 库

Note:This Library valid for Inbox emails in MS Outlook

注意:此库对 MS Outlook 中的收件箱电子邮件有效

回答by Jahmic

Maybe you have long given up on this question, but here goes...

也许你早就放弃了这个问题,但是这里...

I've had this same problem before and I solved it by adding the Outlook Account Management API. Unfortunately for you, this a c++ oriented API. (My addin was already developed in c++)

我以前也遇到过同样的问题,我通过添加 Outlook 帐户管理 API 解决了它。不幸的是,这是一个面向 C++ 的 API。(我的插件已经用 C++ 开发了)

Furthermore, the OOM (Outlook Object Model) which VBA and the .NET addins use has poor (if any) support for multiple accounts. By adding to exchange accounts, you have essentially added multiple accounts to your profile.

此外,VBA 和 .NET 插件使用的 OOM(Outlook 对象模型)对多个帐户的支持很差(如果有的话)。通过添加到交易所帐户,您实际上已将多个帐户添加到您的个人资料中。

So, You might have to go down a level, using MAPI with c++ and then hook in the Outlook Account Management API. It's a lot of work, but that's exactely what I did and it worked like a charm.

因此,您可能需要下一个级别,使用带有 C++ 的 MAPI,然后挂钩 Outlook 帐户管理 API。这是很多工作,但这正是我所做的,它就像一种魅力。

Also, here is an example: http://www.codeproject.com/KB/IP/IOlkAccountManager.aspx

另外,这里有一个例子:http: //www.codeproject.com/KB/IP/IOlkAccountManager.aspx

回答by Christian Flem

I guess this is an old one, but somebody might need it one day. Here is code to iterate all "Sent Mail" folders in Outlook. (I think this will only work for Outlook 2010 and newer).

我想这是一个旧的,但有一天可能有人需要它。这是迭代 Outlook 中所有“已发送邮件”文件夹的代码。(我认为这仅适用于 Outlook 2010 及更新版本)。

MSOutlook._NameSpace ns = Globals.ThisAddIn.Application.GetNamespace("MAPI");
var accounts = ns.Accounts;
foreach (MSOutlook.Account account in accounts)
{
    try
    {
        // You might want to test if DeliveryStore is null, in case this account is not an Exchange account
        MSOutlook.MAPIFolder sentFolder = account.DeliveryStore.GetDefaultFolder(MSOutlook.OlDefaultFolders.olFolderSentMail);
        if(sentFolder != null)
        {
            SentItems = sentFolder.Items;
            SentItems.ItemAdd += LogMethods.Items_Sent_ItemAdd;
        }
    }
    catch (Exception e)
    {
        BaseClass.log.Log(LoggLevel.Warning, e.Message);
    }
}