【问题标题】:Excel VBA Code to retrieve e-mails from outlookExcel VBA 代码从 Outlook 中检索电子邮件
【发布时间】: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

【问题讨论】:

标签: excel vba outlook


【解决方案1】:

只需遍历Inbox 中的所有文件夹。
像这样的东西会起作用。

Edit1:这将避免空白行。

Sub test()
    Dim olApp As Outlook.Application, olNs As Outlook.Namespace
    Dim olFolder As Outlook.MAPIFolder, olMail As Outlook.MailItem
    Dim eFolder As Outlook.Folder '~~> additional declaration
    Dim i As Long
    Dim x As Date, ws As Worksheet '~~> declare WS variable instead
    Dim lrow As Long '~~> additional declaration

    Set ws = Activesheet '~~> or you can be more explicit using the next line
    'Set ws = Thisworkbook.Sheets("YourTargetSheet")
    Set olApp = New Outlook.Application
    Set olNs = olApp.GetNamespace("MAPI")
    x = Date

    For Each eFolder In olNs.GetDefaultFolder(olFolderInbox).Folders
        'Debug.Print eFolder.Name
        Set olFolder = olNs.GetDefaultFolder(olFolderInbox).Folders(eFolder.Name)
        For i = olFolder.Items.Count To 1 Step -1
            If TypeOf olFolder.Items(i) Is MailItem Then
                Set olMail = olFolder.Items(i)
                If InStr(olMail.Subject, "transactions") > 0 _
                And InStr(olMail.ReceivedTime, x) > 0 Then
                    With ws
                       lrow = .Range("A" & .Rows.Count).End(xlup).Row
                       .Range("A" & lrow).Offset(1,0).value = olMail.Subject
                       .Range("A" & lrow).Offset(1,1).Value = olMail.ReceivedTime
                       .Range("A" & lrow).Offset(1,2).Value = olMail.SenderName
                    End With
                End If
            End If
        Next i
        Set olFolder = Nothing
    Next eFolder
End Sub

Above 负责处理Inbox 中的所有子文件夹。
这是你正在尝试的吗?

【讨论】:

  • 非常感谢,现在我必须尝试使其只检查今天的电子邮件,然后再使用其他条件,因为现在有很多电子邮件,所以它工作缓慢。
  • 你好,输出的数据是正确的,但是在没有条件的电子邮件的地方,Excel中有一个空的地方,这导致检索到的电子邮件之间有空行。你也许有知道我该如何解决这个问题?
  • 修复空行:将 j = j + 1 向上移动 2 行。
  • @ArturRutkowski 查看我的编辑。最好的方法是直接访问最后一个空行。
  • 我知道我参加聚会有点晚了,但只是试了一下,它停在 269 行,虽然我不知道为什么。有什么想法吗?
【解决方案2】:

修复您的错误(olFolderInbox 是 Outlook 唯一的常量,因此您需要在不是 Outlook 的 vba 中定义它):

Const olFolderInbox = 6
'...
Set Fldr = olNs.GetDefaultFolder(olFolderInbox).Folders("PRE Customer")

另外为了防止在另一台计算机上运行时丢失参考,我会:

Dim olApp As Object
Dim olNs As Object
Dim Fldr As Object
Dim olMail As Object
Dim i As Long
Set olApp = CreateObject("Outlook.Application")
'...

您可能还想禁用 ScreenUpdating,然后在 Excel 中启用它(如果您希望列表很长)。


更新(根文件夹中所有文件夹的解决方案)

我使用了一些稍微不同的方法来比较日期。

Option Explicit

Private lRow As Long, x As Date, oWS As Worksheet

Sub GetFromInbox()
    Const olFolderInbox = 6
    Dim olApp As Object, olNs As Object
    Dim oRootFldr As Object ' Root folder to start
    Dim lCalcMode As Long

    Set olApp = CreateObject("Outlook.Application")
    Set olNs = olApp.GetNamespace("MAPI")
    Set oRootFldr = olNs.GetDefaultFolder(olFolderInbox).Folders("PRE Customer")
    Set oWS = ActiveSheet

    x = Date
    lRow = 1
    lCalcMode = Application.Calculation
    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False
    GetFromFolder oRootFldr
    Application.ScreenUpdating = True
    Application.Calculation = lCalcMode

    Set oWS = Nothing
    Set oRootFldr = Nothing
    Set olNs = Nothing
    Set olApp = Nothing
End Sub

Private Sub GetFromFolder(oFldr As Object)
    Dim oItem As Object, oSubFldr As Object

    ' Process all mail items in this folder
    For Each oItem In oFldr.Items
        If TypeName(oItem) = "MailItem" Then
            With oItem
                If InStr(1, .Subject, "transactions", vbTextCompare) > 0 And DateDiff("d", .ReceivedTime, x) = 0 Then
                    oWS.Cells(lRow, 1).Value = .Subject
                    oWS.Cells(lRow, 2).Value = .ReceivedTime
                    oWS.Cells(lRow, 3).Value = .SenderName
                    lRow = lRow + 1
                End If
            End With
        End If
    Next

    ' Recurse all Subfolders
    For Each oSubFldr In oFldr.Folders
        GetFromFolder oSubFldr
    Next
End Sub

【讨论】:

  • 非常感谢,我今天也要试试这个。干杯
  • 您可以递归到收件箱中的所有文件夹,但您需要知道 Excel 中的文件夹路径吗?或者 EntryID,如果您想稍后引用它?
  • 无论来自哪个 Outlook 文件夹,我都只需要电子邮件属性(主题、时间、发件人),只需要在主题和今天添加诸如“单词”之类的条件(我想这样做首先检查今天的日期以便立即不要检查日期是否不是今天)
  • @ArturRutkowski 在给定根文件夹的情况下尝试递归模式的更新代码。
  • 当olFldr 中的olMail 不是MailItem 时,这可能会产生错误。尽管为LateBinding +1,因为正如OP指出的那样,这将被多个用户使用。这将消除版本差异。
猜你喜欢
  • 2018-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-28
  • 2016-06-24
相关资源
最近更新 更多