【发布时间】:2014-05-19 20:09:31
【问题描述】:
我将编写一个 VBA 代码,该代码将根据特定条件从 Outlook 检索电子邮件。我遇到的问题是我必须在我的代码中表示某个文件夹(在下面的示例中,该文件夹表示为“PRE Costumer”。我想从我的“收件箱”或更好的情况下从所有 Outlook 文件夹中检索所有电子邮件.问题是我的收件箱包含许多子文件夹(因为rules0。我的问题是我可能不知道所有子文件夹的名称(因为许多用户将使用宏,甚至有人可以在个人文件夹中拥有电子邮件) .
请问有没有办法解决这个问题?
如果这个问题含糊不清,请告诉我(因为我是新手)
请找到我有问题的行,并带有注释。
Sub GetFromInbox()
Dim olApp As Outlook.Application
Dim olNs As Namespace
Dim Fldr As MAPIFolder
Dim olMail As Variant
Dim i As Integer
Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
'Below is the line I have problem with
Set Fldr = olNs.GetDefaultFolder(olFolderInbox).Folders("PRE Customer")
i = 1
x = Date
For Each olMail In Fldr.Items
If InStr(olMail.Subject, "transactions") > 0 _
And InStr(olMail.ReceivedTime, x) > 0 Then
ActiveSheet.Cells(i, 1).Value = olMail.Subject
ActiveSheet.Cells(i, 2).Value = olMail.ReceivedTime
ActiveSheet.Cells(i, 3).Value = olMail.SenderName
i = i + 1
End If
Next olMail
Set Fldr = Nothing
Set olNs = Nothing
Set olApp = Nothing
End Sub
【问题讨论】:
-
由于 Outlook 的安全系统,从 Outlook 写入 Excel 比从 Excel 读取 Outlook 容易得多。我的这两个答案可以让你开始。 How to copy Outlook mail message into excel using VBA or MacrosHow to import the outlook mail data to Excel