尝试使用来自另一个线程的vba,但是,在“forFlInIn.Items中的每个olMail”行中获取不匹配类型。我有什么想法我做错了吗?我有outlook和excel引用。
Dim olMail As MailItem
Dim aOutput() As Variant
Dim lCnt As Long
Dim xlApp As Excel.Application
Dim xlSh As Excel.Worksheet
Dim flInbox As Folder
Set flInbox = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
ReDim aOutput(1 To flInbox.Items.Count, 1 To 4)
For Each olMail In flInbox.Items
If TypeName(olMail) = "MailItem" Then
lCnt = lCnt + 1
aOutput(lCnt, 1) = olMail.SenderEmailAddress 'maybe stats on domain
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
End If
Next olMail
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
链接:
Using VBA in Outlook to gather statistics on received emails
答案 0 :(得分:1)
You For Each循环假定从Items集合的索引器获取的对象将MailItem对象返回到olMail变量中。该文件夹可能包含非电子邮件消息,例如ReportItem或MeetingRequest对象。而是声明一个Object变量,并在将其转换为特定对象时间之前检查MessageClass或Type的值以验证该消息是否为MailItem对象。我还会使用带有索引器的For循环而不是For Each,这会在迭代Outlook对象时消耗更少的内存资源。