Excel VBA处理和转发引起“内存不足”错误的电子邮件

我希望有人可以帮助 – 我遇到了可怕的“内存不足或系统资源”的错误与运行在Excel中的一些代码和Outlook的工作; 从哪个错误发源。

简短描述是通过查看身体/主题的电子邮件列表来查看。 如果find它,它会转发电子邮件项目与主题中的参考。 MWE以下; 我不是很有经验的处理Outlook对象,但我花了近两个小时尝试不同的东西,没有运气。 我不能使用GetTable()函数,因为它不包含正文文本数据据我所知(工作了这个 ),除非你可以添加列以包含正文文本?

如果我只用十几个项目在新开的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)它实际上是为特定的邮件项目? 如果我给它一批这些问题消息之一,立即挂起。 这可能是与字符编码或消息长度有关吗? 有什么意思thisMail.Body将无法正常工作,触发资源错误?

问题的原因:

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

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

一旦你完成对象,释放对象

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

通用的语言解释

在这种情况下,想想VBA作为一个服务员,你告诉它,你会给一些菜服务给客户,你把所有的人都给了它,但是你永远不会告诉它把它们放到桌子上,有一点,它将无法处理更多的菜肴(“内存不足”)