Excel VBA处理和转发电子邮件导致"内存不足"错误

时间:2017-06-08 01:18:12

标签: excel vba excel-vba outlook outlook-vba

我希望有人可以提供帮助 - 我会遇到可怕的内存或系统资源问题"某些代码在Excel中运行并使用Outlook时出错;从中产生错误。

简短描述是通过查看正文/主题的电子邮件列表进行参考。如果找到它,它会转发包含主题中引用的电子邮件项目。 MWE低于;我处理Outlook对象的经验并不是很有经验,但我花了将近两个小时尝试不同的事情而没有运气。我不能使用GetTable()函数,因为据我所知它没有包含Body文本数据(关闭this),除非你能以某种方式添加列来包含正文文本?

如果我在一个只有十几个项目的新开放的Outlook会话中运行它,这不是一个问题,但我需要它在一个pop中处理数百封电子邮件。我的头靠在墙上。非常感谢提前!

Private Sub processMWE(ByVal oParent As Outlook.MAPIFolder)
        Dim thisMail As Outlook.MailItem
        Dim myItems As Outlook.Items
        Dim emailindex As Integer
        Dim folderpath As String
        Dim refandType As Variant
        Dim fwdItem
        Set myItems = oParent.Items
        folderpath = oParent.folderpath

        'Starting at row 2 on the current sheet
        i = 2

        With myItems
            'Data output to columns in Excel
            For emailindex = 1 To .Count
                Set thisMail = .Item(emailindex)

                'i takes row value
                Cells(i, 1).Value = folderpath
                Cells(i, 2).Value = thisMail.Subject + " " + thisMail.Body
                Cells(i, 3).Value = thisMail.SenderEmailAddress
                Cells(i, 4).Value = thisMail.ReceivedTime
                Cells(i, 6).Value = thisMail.Categories

                'Reference from body/subject and a match type (integer)
                refandType = extractInfo(Cells(i, 2))

                'This is the reference
                Cells(i, 5).Value = refandType(0)

                'And this is the match type.
                Select Case refandType(1)
                Case 1, 2
                    'do nothing
                Case Else
                    'For these match types, fwd the message
                    Set fwdItem = thisMail.Forward
                    fwdItem.Recipients.Add "#####@###"
                    fwdItem.Subject = Cells(i, 5) & " - " & thisMail.Subject
                    fwdItem.Send
                    'Edit original message category label
                    thisMail.Categories = "Forwarded"
                    thisMail.Save
                    'Note in spreadsheet
                    Cells(i, 7).Value = "Forwarded"
                    End If
                End Select
                i = i + 1
            Next
        End With
End Sub

编辑:新开发:它不仅总是挂在同一行代码(thisMail.Body)上,它实际上是针对特定邮件项目做的吗?!如果我给它一批这些问题消息,它会立即挂起。它可能与字符编码或消息长度有关吗?这意味着这个邮件.Body工作会导致资源错误吗?

1 个答案:

答案 0 :(得分:1)

问题原因:

您正在创建项目而不从内存中释放它们 - 使用这些行 -

        For emailindex = 1 To .Count
            Set thisMail = .Item(emailindex)

<强> 解决方案

完成对象后释放对象

 End Select
                i = i + 1
 Set thisMail = Nothing           
 Next
        End With

通用语言解释

在这种情况下,想想VBA作为服务员,你告诉它你要给一些菜肴服务给顾客,你给他们所有人,但你永远不会告诉它将它们释放给桌子,在某一点上,它将无法处理任何更多的菜肴(&#34;内存不足和#34;)

相关问题