在MS Outlook VBA中,我如何测试一个文件夹是否用于电子邮件?

huangapple go评论74阅读模式
英文:

In MS Outlook VBA, how can I test if a folder is for e-mails?

问题

我想遍历所有我的Outlook文件夹,并在每个文件夹中构建一个电子邮件Table。但是,要向表格添加电子邮件属性列的代码,例如:

Set oTable = oFolder.GetTable(sFilter)
oTable.Columns.Add ("SenderName")

对于不包含电子邮件的文件夹(例如Calendar文件夹),会失败。

我可以通过检查文件夹中的第一项是否为电子邮件来判断其class是否为电子邮件:

oFolder.Items.Item(1).class = olMail

但这不能作为有效的测试,因为电子邮件文件夹可以包含不是电子邮件的项目,例如MeetingItem。如果电子邮件文件夹中的第一项恰好是MeetingItem,那么上述测试将错误地判断该文件夹不是电子邮件文件夹。

那么,我该如何测试文件夹是否为电子邮件文件夹,以便知道是否可以向由它创建的表格中添加电子邮件属性?

英文:

I want to run through all my Outlook folders and build a Table of e-mails in each. But the code to add columns of e-mail properties to the table, such as:

Set oTable = oFolder.GetTable(sFilter)
oTable.Columns.Add ("SenderName")

fails on folders that don't hold e-mails, such as the folder Calendar.

I could check if the first item in the folder is an e-mail by checking its class:

oFolder.Items.Item(1).class = olMail

But that cannot be a valid test because an e-mail folder can contain items that are not e-mails, such as a MeetingItem. If the first item in an e-mail folder happens to be a MeetingItem, then the above test would give a false negative on whether the folder is an e-mail folder.

So how can I test if a folder is an e-mail folder so I can know whether I can add e-mail properties to a table made from it?

答案1

得分: 1

您可以使用Folder.DefaultItemType属性,该属性返回OlItemType枚举中的常量,指示文件夹中包含的默认Outlook项目类型。

实际上,Outlook文件夹可能包含不同类型的项目 - 邮件、约会、文档、帖子等。您最好的做法是检查文件夹的默认项目类型。

英文:

You can use the Folder.DefaultItemType property which returns a constant from the OlItemType enumeration indicating the default Outlook item type contained in the folder.

Actually, Outlook folders may contain different type of items - mail, appointment, document, post and etc. The best what you could do is to check the default item type for the folder.

答案2

得分: 1

无法通过其中一个项目来判断文件夹类型 - 例如,"已删除项目"文件夹可以包含任何类型的项目。收件箱通常包含MailItemReportItemMeetingItem对象。日历文件夹只包含AppointmentItem对象,但联系人文件夹可以包含ContactItemDistListItem对象。

您可能希望提供确切的详细信息,以了解您正在尝试实现什么目标。

英文:

You cannot judge the folder type by one of its items - e.g., Deleted Items folder can contain any kind of item. Inbox usually contains MailItem, ReportItem, MeetingItem objects. Calendar folder only contains AppointmentItem objects, but Contacts folder can contain both ContactItem and DistListItem objects.

You might want to provide details what it is exactly you are trying to achieve.

huangapple
  • 本文由 发表于 2023年5月24日 23:04:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76324957.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定