我有下面的代码,从outlook文件夹中提取数据,并选择日期范围或限制日期.然而,我试图从“多个outlook文件夹”中提取数据,但下面的代码只允许我一次从“1”文件夹中选择。
我怎么可能选择更多的文件夹或循环此添加更多的数据从其他文件夹?救命啊!我到处都找遍了,找不到解决办法。
Sub getDataFromOutlookChoiceFolder()
Dim OutlookApp As Outlook.Application
Dim OutlookNameSpace As Namespace
Dim Folder As MAPIFolder
Dim OutlookMail As Variant
Dim i As Long
Set OutlookApp = New Outlook.Application
Set OutlookNameSpace = OutlookApp.GetNamespace("MAPI")
Set Folder = OutlookNameSpace.PickFolder
If Folder.Items.Count = 0 Then
MsgBox "No emails. Existing procedure!"
Exit Sub
End If
i = 1
Dim rngName As Name
Sheet1.Cells.Clear
For Each rngName In ActiveWorkbook.Names
rngName.Delete
Next
Range("A1").Name = "receivedtime"
Range("A1") = "Received Time"
Range("B1").Name = "From"
Range("B1") = "From"
Range("C1").Name = "To"
Range("C1") = "To"
Range("D1").Name = "Subject"
Range("D1") = "Subject"
Range("E1").Name = "Body"
Range("E1") = "Body"
Range("F1").Name = "Conversation_ID"
Range("F1") = "Conversation ID"
Range("G1").Name = "email_Receipt_Date"
Range("G2").Name = "email_end_date"
Range("email_Receipt_Date").Value = InputBox("Enter Receipt Date like DD-Mon-YYYY")
Range("email_end_date").Value = InputBox("Enter Receipt Date like DD-Mon-YYYY")
For Each OutlookMail In Folder.Items
If OutlookMail.ReceivedTime >= Range("email_Receipt_Date").Value And OutlookMail.ReceivedTime <= Range("email_end_date").Value Then
Range("receivedtime").Offset(i, 0).Value = OutlookMail.ReceivedTime
Range("receivedtime").Offset(i, 0).Columns.AutoFit
Range("receivedtime").Offset(i, 0).VerticalAlignment = xlTop
Range("from").Offset(i, 0).Value = OutlookMail.SenderName
Range("from").Offset(i, 0).Columns.AutoFit
Range("from").Offset(i, 0).VerticalAlignment = xlTop
Range("to").Offset(i, 0).Value = OutlookMail.To
Range("to").Offset(i, 0).Columns.AutoFit
Range("to").Offset(i, 0).VerticalAlignment = xlTop
Range("subject").Offset(i, 0).Value = OutlookMail.Subject
Range("subject").Offset(i, 0).Columns.AutoFit
Range("subject").Offset(i, 0).VerticalAlignment = xlTop
Range("body").Offset(i, 0).Value = OutlookMail.Body
Range("body").Offset(i, 0).Columns.AutoFit
Range("body").Offset(i, 0).VerticalAlignment = xlTop
Range("Conversation_ID").Offset(i, 0).Value = OutlookMail.ConversationID
Range("Conversation_ID").Offset(i, 0).Columns.AutoFit
Range("Conversation_ID").Offset(i, 0).VerticalAlignment = xlTop
i = i + 1
End If
Next OutlookMail
Set Folder = Nothing
Set OutlookNameSpace = Nothing
Set OutlookApp = Nothing
MsgBox ("Completed")
End Sub
2条答案
按热度按时间isr3a4wc1#
首先,迭代循环中的所有项以获取特定日期的项是不正确的。相反,您需要使用
Items
类的Find
/FindNext
或Restrict
方法。你可以在我为技术博客写的文章中阅读更多关于这些方法的内容:如果你需要处理所有的子文件夹,你必须递归地遍历子文件夹。例如:
但是一个更好的解决方案,满足您的所有需求是
Application
类的AdvancedSearch
方法。在Outlook中使用AdvancedSearch
方法的主要优点是:AdvancedSearch
方法会在后台自动运行它。Restrict
和Find
/FindNext
方法可以应用于特定的Items
集合(请参阅Outlook中Folder
类的Items
属性)。Search
类的Stop
方法停止搜索过程。在Advanced search in Outlook programmatically: C#, VB.NET文章中阅读更多关于它的内容。
7lrncoxx2#
编辑
在循环之前初始化计数器i。我不小心把它留在了原来的地方。
原创
这比你想象的要简单我将您的代码放在
Do ... Loop
中,以允许用户继续选择文件夹,直到用户单击Cancel。它退出循环。我删除了你的大部分报告线只是为了保持这一点。一旦你确信这是有效的,就把它们加回去。