【问题标题】:Iterate through Outlook subfolders and inbox遍历 Outlook 子文件夹和收件箱
【发布时间】:2016-01-15 05:55:44
【问题描述】:

我在下面有这段代码,它在收件箱中循环,搜索在工作表的 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

我不确定如何遍历子文件夹。我已经检查过,并从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

【问题讨论】:

    标签: vba excel outlook


    【解决方案1】:

    私有 sub 位于一个模块中,并且只对该模块可用,您可以通过以下方式调用 sub:

    Call processFolder(The Outlook.MAPIFolder)
    

    这个 sub 需要一个输入变量oParent,它的形式是 Outlook.MAPIFolder。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-28
      • 1970-01-01
      • 1970-01-01
      • 2018-09-05
      • 2016-02-12
      相关资源
      最近更新 更多