共享收件箱 - 跳过 Outlook VBA 中的 non-mail 个项目

Shared Inbox - Skipping non-mail items in Outlook VBA

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

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

一般来说,我还有另一个关于宏的问题。有时它不会 运行 因为它似乎无法识别收件箱的 "ARCHIVE" sub-folder 但其他时候它很好。我的猜测是,当共享收件箱与服务器同步时,或类似的情况,无法访问 "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

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