迭代Outlook子文件夹和收件箱

时间:2016-01-14 21:39:04

标签: excel vba excel-vba outlook

我在下面有这个代码,它循环通过收件箱,搜索在工作表的列E中输入的特定电子邮件地址。它将最后一个电子邮件发送日期返回到b列。

Sub ()

Dim olApp As Outlook.Application
Dim olNs As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder
Dim olMail As Outlook.MailItem
Dim eFolder As Outlook.Folder
Dim i As Long
Dim wb As Workbook
Dim ws As Worksheet
Dim icounter As Long
Dim lrow As Long

Set wb = ActiveWorkbook
Set ws = wb.Worksheets("-")

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

lastrow = ThisWorkbook.Worksheets("vlookup").Cells(Rows.Count, "E").End(xlUp).Row

Set olFolder = olNs.GetDefaultFolder(olFolderInbox)
For i = olFolder.Items.Count To 1 Step -1

 If TypeOf olFolder.Items(i) Is MailItem Then
        Set olMail = olFolder.Items(i)
            For icounter = 2 To lastrow
            If InStr(olMail.SenderEmailAddress, ws.Cells(icounter, 5).Value) > 0 Then 'qualify the cell
                With ws
                   lrow = .Range("A" & .Rows.Count).End(xlUp).Row
                       .Range("B" & lrow + 1).Value = olMail.ReceivedTime
                       .Range("A" & lrow + 1).Value = olMail.SenderEmailAddress
                End With
            End If
            Next icounter
        End If
    Next i
    Set olFolder = Nothing



    End Sub

我不确定如何遍历子文件夹。我已经检查了SO,并在Can I iterate through all Outlook emails in a folder including sub-folders?

中找到了以下代码
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
            Next
        End If
End Sub

但是我从未使用私人潜艇,所以我不知道如何将它们结合起来。

还发现这是使用上面找到的私有子组合的组合版本,但我没有把它翻译成我的代码。 Outlook VBA Importing Emails from Subfolders into Excel

1 个答案:

答案 0 :(得分:0)

private sub位于模块中,仅对该模块可用,您可以通过写入来调用sub:

Call processFolder(The Outlook.MAPIFolder)

此子要求输入变量oParent,其形式为Outlook.MAPIFolder。

相关问题