vba - 共享收件箱 - 跳过 Outlook VBA 中的非邮件项目

标签 vba outlook mailitem

我不是 Outlook VBA 方面的专家,但我已经设法创建了一些运行良好的宏。我已经在下面的代码上工作了一段时间,现在只剩下一个小问题。该宏将每封电子邮件的信息从共享收件箱的子文件夹导入到 Excel 文件中。我遇到的问题是 for next 循环遇到非邮件项目(例如 session 邀请或投递失败通知)。代码在“下一步”行停止,并在遇到这些非邮件项目时给出“类型不匹配”错误。再次按下播放继续代码,直到它遇到另一个非邮件项目。我想让代码跳过这些非邮件项目并循环遍历整个收件箱/文件夹。

我已经尝试过“On Error Resume Next”,但它似乎跳过了“Next”行并继续执行剩余的代码,而没有实际循环回到“For Each”行。我玩过 If's 和 GoTo 语句,但没有一个对我有用。有人可以帮忙吗?

一般来说,我还有另一个关于宏的问题。有时它不会运行,因为它似乎无法识别收件箱的“ARCHIVE”子文件夹,但其他时候没问题。我的猜测是,当共享收件箱与服务器同步时,或类似的东西,无法访问“ARCHIVE”文件夹,但这只是一个猜测。如果有人能对这个问题有更多的了解,我也将不胜感激。

Sub EmailStatsV3()

Dim olMail As Outlook.MailItem
Dim aOutput() As Variant
Dim lCnt As Long
Dim xlApp As Excel.Application
Dim xlSh As Excel.Worksheet
Dim flInbox As Folder

'Gets the mailbox and shared folder inbox
Dim myNamespace As Outlook.NameSpace
Dim myRecipient As Outlook.Recipient
Set myNamespace = Application.GetNamespace("MAPI")
Set myRecipient = myNamespace.CreateRecipient("Shared Inbox") 'Change "Shared Inbox" to whatever shared inbox you use

Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objInbox = objNamespace.GetSharedDefaultFolder(myRecipient, olFolderInbox)

'Uses the Parent of the Inbox to specify the mailbox
strFolderName = objInbox.Parent

'Specifies the folder (inbox or other) to pull the info from
Set objMailbox = objNamespace.Folders(strFolderName)
Set objFolder = objMailbox.Folders("Inbox").Folders("ARCHIVE") 'Change this line to specify folder
Set colItems = objFolder.Items

'Specify which email items to extract
ReDim aOutput(1 To objFolder.Items.Count, 1 To 5)
For Each olMail In objFolder.Items
If TypeName(olMail) = "MailItem" Then

        lCnt = lCnt + 1
        aOutput(lCnt, 1) = olMail.SenderEmailAddress 'Sender or SenderName also gives similar output
        aOutput(lCnt, 2) = olMail.ReceivedTime 'stats on when received
        aOutput(lCnt, 3) = olMail.ConversationTopic 'group based on subject w/o regard to prefix
        aOutput(lCnt, 4) = olMail.Subject 'to split out prefix
        aOutput(lCnt, 5) = olMail.Categories 'to split out category
End If

Next olMail

'Creates a blank workbook in excel then inputs the info from Outlook
Set xlApp = New Excel.Application
Set xlSh = xlApp.Workbooks.Add.Sheets(1)

xlSh.Range("A1").Resize(UBound(aOutput, 1), UBound(aOutput, 2)).Value = aOutput
xlApp.Visible = True


End Sub

最佳答案

改变

Dim olMail As Outlook.MailItem

Dim olMail As Variant

Variant 类型应用于在 For Each 循环中迭代集合,在您的示例中,Next 项目不是 MailItem - 这是 olMail 被声明的内容。您已经检查过 olMail 是否是邮件项,因此您可以在此处使用变体。

关于vba - 共享收件箱 - 跳过 Outlook VBA 中的非邮件项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32741149/

相关文章:

outlook - 在 Office 加载项 list 中指定 ~remoteAppUrl 的值

c# - 从 C# 中的 MailItem 获取命名的 MAPI 属性

vba - 如何从不在 Outlook 中的硬盘驱动器中打开 Outlook .msg 文件?

sql - 在表中 Access VBA 搜索和替换

vba - 如何通过引用两个单元格自动命名电子表格?

excel - 将工作表分配给变量时出现运行时错误 13

outlook - 如何使用 ical.net 创建 HTML 格式的 ICS 消息正文?

vba - XslCompiledTransform - 使用 XSLT 样式表转换 XML 时出现编译错误

python-3.x - 在电子邮件中呈现的 HTML

c# - 如何确定 Exchange 邮件项目是否为自动回复邮件?