【问题标题】:Outlook VBA Importing Emails from Subfolders into ExcelOutlook VBA 将电子邮件从子文件夹导入 Excel
【发布时间】:2016-02-05 07:11:09
【问题描述】:

我正在尝试将收件箱中每封电子邮件的详细信息(发件人、接收时间、主题等)导入 Excel 文件。我的代码适用于收件箱中的特定文件夹,但我的收件箱有几个子文件夹,这些子文件夹也有子文件夹。

经过多次反复试验,我已成功导入收件箱下所有子文件夹的详细信息。但是,该代码不会从第二层子文件夹导入电子邮件,它还会跳过仍在收件箱本身中的电子邮件。我已经搜索了这个站点和其他站点,但找不到循环遍历收件箱的所有文件夹和子文件夹的代码。

例如,我有一个包含报告、定价和项目子文件夹的收件箱。 报告子文件夹有名为 Daily、Weekly 和 Monthly 的子文件夹。我可以在报告中导入电子邮件,但不能在每日、每周和每月中导入。

我的代码如下:

Sub SubFolders()

Dim olMail As Variant
Dim aOutput() As Variant
Dim lCnt As Long
Dim xlSh As Excel.Worksheet
Dim olApp As Outlook.Application
Dim olNs As Folder
Dim olParentFolder As Outlook.MAPIFolder
Dim olFolderA As Outlook.MAPIFolder
Dim olFolderB As Outlook.MAPIFolder

Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)

Set olParentFolder = olNs
ReDim aOutput(1 To 100000, 1 To 5)

For Each olFolderA In olParentFolder.Folders
    For Each olMail In olFolderA.Items
    If TypeName(olMail) = "MailItem" Then
    On Error Resume Next
        lCnt = lCnt + 1
        aOutput(lCnt, 1) = olMail.SenderEmailAddress
        aOutput(lCnt, 2) = olMail.ReceivedTime
        aOutput(lCnt, 3) = olMail.Subject
        aOutput(lCnt, 4) = olMail.Sender
        aOutput(lCnt, 5) = olMail.To

    End If
    Next
Next

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

【问题讨论】:

  • 谢谢。我使用了链接中给出的代码,它导入了 Outlook 中的所有内容。虽然这很有用,但它提供了太多信息。我希望我可以指定一个文件夹(例如收件箱)并从中导入所有内容及其子文件夹。不知道是否可以修改上面的代码来实现这个?

标签: vba outlook subdirectory


【解决方案1】:

来自这个问题Can I iterate through all Outlook emails in a folder including sub-folders?

替换您迭代文件夹的尝试...

For Each olFolderA In olParentFolder.Folders
    For Each olMail In olFolderA.Items
    If TypeName(olMail) = "MailItem" Then
    On Error Resume Next
        lCnt = lCnt + 1
        aOutput(lCnt, 1) = olMail.SenderEmailAddress
        aOutput(lCnt, 2) = olMail.ReceivedTime
        aOutput(lCnt, 3) = olMail.Subject
        aOutput(lCnt, 4) = olMail.Sender
        aOutput(lCnt, 5) = olMail.To
    End If
    Next
Next

...使用当前接受的答案中描述的递归思想。

Private Sub processFolder(ByVal oParent As Outlook.MAPIFolder)
    Dim oFolder As Outlook.MAPIFolder
    Dim oMail As Outlook.MailItem

    For Each oMail In oParent.Items

    'Get your data here ...

    Next

    If (oParent.Folders.Count > 0) Then
        For Each oFolder In oParent.Folders
            processFolder oFolder   ' <--- no brackets around oFolder
        Next
    End If
End Sub

充实的第二个答案显示了如何在代码之外声明变量以传递值。

Option Explicit

Dim aOutput() As Variant
Dim lCnt As Long

Sub SubFolders()
'
' Code for Outlook versions 2007 and subsequent
' Declare with Folder rather than MAPIfolder
'
Dim xlApp As Excel.Application
Dim xlSh As Excel.Worksheet

Dim olNs As Namespace
Dim olParentFolder As Folder

Set olNs = GetNamespace("MAPI")
Set olParentFolder = olNs.GetDefaultFolder(olFolderInbox)

lCnt = 0
ReDim aOutput(1 To 100000, 1 To 5)

ProcessFolder olParentFolder

On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
On Error GoTo 0
If xlApp Is Nothing Then Set xlApp = CreateObject("Excel.Application")

Set xlSh = xlApp.Workbooks.Add.Sheets(1)

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

ExitRoutine:
    Set olNs = Nothing
    Set olParentFolder = Nothing
    Set xlApp = Nothing
    Set xlSh = Nothing

End Sub

Private Sub ProcessFolder(ByVal oParent As Folder)

Dim oFolder As Folder
Dim oMail As Object

For Each oMail In oParent.Items

    If TypeName(oMail) = "MailItem" Then
        lCnt = lCnt + 1
        aOutput(lCnt, 1) = oMail.SenderEmailAddress
        aOutput(lCnt, 2) = oMail.ReceivedTime
        aOutput(lCnt, 3) = oMail.Subject
        aOutput(lCnt, 4) = oMail.Sender
        aOutput(lCnt, 5) = oMail.To
    End If

Next

If (oParent.Folders.count > 0) Then
    For Each oFolder In oParent.Folders
        ProcessFolder oFolder
    Next
End If

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多