I have some code in Outlook that reads through an email folder that you specify and with the dates you give it.. write data to an excel file. I'm trying to run it against a folder with 250 emails in it and it id DYING on this line with an OUT OF MEMORY error:

VB Code:
  1. xlSheet.Cells(localRowCount, 4) = Replace(olTempItem.Body, Chr(9), " ")

Here is my code.... can anyone suggest a way to make this run without choking???

VB Code:
  1. Dim strMessageBody As String
  2. Dim strAttachment As String
  3. Dim dtStartDate As Date
  4. Dim dtEndDate As Date
  5. Dim globalRowCount As Long
  6.  
  7. Dim xlApp As Excel.Application
  8. Dim xlBook As Excel.Workbook
  9. Dim xlSheet As Excel.Worksheet
  10.  
  11. Option Explicit
  12.  
  13. Sub Export()
  14.    
  15.    Dim olApp As Outlook.Application
  16.    Dim olSession As Outlook.NameSpace
  17.    Dim olStartFolder As Outlook.MAPIFolder
  18.    Dim olDestFolder As Outlook.MAPIFolder
  19.    Dim strprompt As String
  20.    Dim recipient As String
  21.    Dim localRowCount As Integer
  22.    
  23.    
  24.    Set xlApp = CreateObject("Excel.Application")
  25.    
  26.    'Initialize count of folders searched
  27.    globalRowCount = 1
  28.    
  29.    ' Get a reference to the Outlook application and session.
  30.    Set olApp = Application
  31.    Set olSession = olApp.GetNamespace("MAPI")
  32.  
  33.    ' Allow the user to input the start date
  34.    strprompt = "Enter the start date to search from:"
  35.    dtStartDate = InputBox(strprompt, "Start Date", Now() - 7)
  36.  
  37.    ' Allow the user to input the end date
  38.    strprompt = "Enter the end date to search to:"
  39.    dtEndDate = InputBox(strprompt, "End Date", Now())
  40.    
  41. '   UserForm1.Show
  42.    
  43.    
  44.    If (IsNull(dtStartDate) <> 1) And (IsNull(dtEndDate) <> 1) Then
  45.  
  46.       ' Allow the user to pick the folder in which to start the search.
  47.       MsgBox ("Pick the source folder (Feedback)")
  48.       Set olStartFolder = olSession.PickFolder
  49.      
  50.       ' Check to make sure user didn't cancel PickFolder dialog.
  51.       If Not (olStartFolder Is Nothing) Then
  52.          ' Start the search process.
  53.          ProcessFolder olStartFolder
  54.          MsgBox CStr(globalRowCount) & " messages were found."
  55.       End If
  56.    
  57.    xlApp.Quit
  58.  
  59.    
  60. '   MsgBox "Email sent to " & recipient
  61.    MsgBox "Process is complete.  Check K:\feedback\htm\ for available files."
  62.  
  63.    End If
  64. End Sub
  65.  
  66. Sub ProcessFolder(CurrentFolder As Outlook.MAPIFolder)
  67.  
  68. Dim i As Long
  69. Dim ValidEmails As Long
  70. ValidEmails = 0
  71.  
  72. For i = CurrentFolder.Items.Count To 1 Step -1
  73.    If ((CurrentFolder.Items(i).ReceivedTime >= dtStartDate) And (CurrentFolder.Items(i).ReceivedTime < dtEndDate)) Then
  74.    ValidEmails = ValidEmails + 1
  75.    End If
  76. Next
  77.  
  78. If CurrentFolder.Items.Count >= 1 And ValidEmails >= 1 Then
  79.    
  80.    Dim localRowCount As Integer
  81.    Dim xlName As String
  82.      
  83.    Set xlBook = xlApp.Workbooks.Add
  84.    Set xlSheet = xlBook.Worksheets(1)
  85.      
  86.    localRowCount = 1
  87.    xlName = CStr(Format(dtStartDate, "MMDDYYYY")) & "_" & CurrentFolder.Name & "_feedback"
  88.  
  89.    xlSheet.Cells(localRowCount, 1) = "SUBJECT"
  90.    xlSheet.Cells(localRowCount, 2) = "SENDER"
  91.    xlSheet.Cells(localRowCount, 3) = "RECEIVED DATE"
  92.    xlSheet.Cells(localRowCount, 4) = "MESSAGE BODY"
  93.  
  94.    
  95.    ' Late bind this object variable,
  96.    ' since it could be various item types
  97.    Dim olTempItem As Object
  98.    Dim olNewFolder As Outlook.MAPIFolder
  99.  
  100.  
  101.    ' Loop through the items in the current folder.
  102.    ' Looping through backwards in case items are to be deleted,
  103.    ' as this is the proper way to delete items in a collection.
  104.        For i = CurrentFolder.Items.Count To 1 Step -1
  105.    
  106.           Set olTempItem = CurrentFolder.Items(i)
  107.    
  108.           ' Check to see if a match is found
  109.           If ((olTempItem.ReceivedTime >= dtStartDate) And (olTempItem.ReceivedTime < dtEndDate)) Then
  110.             localRowCount = localRowCount + 1
  111.             globalRowCount = globalRowCount + 1
  112.             xlSheet.Cells(localRowCount, 1) = olTempItem.Subject
  113.             xlSheet.Cells(localRowCount, 2) = olTempItem.SenderEmailAddress
  114.             xlSheet.Cells(localRowCount, 3) = CStr(Format(olTempItem.ReceivedTime, "MM/DD/YYYY"))
  115.     '       Added this row of Code 4/3/06 jmr
  116.     '       xlSheet.Cells(localRowCount, 4) = WorksheetFunction.Clean(olTempItem.Body)
  117.     '       Remarked out the above row and added below row back in 6-7-06
  118.     '       xlSheet.Cells(localRowCount, 4) = Replace(Replace(Replace(olTempItem.Body, Chr(9), " "), Chr(10) & Chr(10), Chr(10)), Chr(13), "")
  119.            
  120.            
  121.             xlSheet.Cells(localRowCount, 4) = Replace(olTempItem.Body, Chr(9), " ")
  122.             xlSheet.Cells(localRowCount, 4) = Replace(olTempItem.Body, Chr(10) & Chr(10), Chr(10))
  123.             xlSheet.Cells(localRowCount, 4) = Replace(olTempItem.Body, Chr(13), "")
  124.            
  125.           End If
  126.        
  127.        Next
  128.    
  129.    readability_and_HTML_export
  130.    xlBook.SaveAs ("\\stm-fs1\marketing-shared\feedback\xls\" & xlName & ".xls")
  131.    
  132.   ' Orig Code Block
  133.      ' ActiveWorkbook.PublishObjects.Add( _
  134.      ' SourceType:=xlSourceSheet, _
  135.      ' FileName:="\\stm-fs1\marketing-shared\feedback\htm\" & xlName & ".htm", _
  136.      ' Sheet:="Sheet1", _
  137.      ' Source:="", _
  138.      ' HtmlType:=xlHtmlStatic).Publish
  139.    
  140.     ' Added the next THREE lines of code to replace ABOVE code block 050706 jmr
  141.       ActiveSheet.SaveAs _
  142.       FileName:="\\stm-fs1\marketing-shared\feedback\htm\" & xlName & ".htm", _
  143.       FileFormat:=xlHtml
  144.    
  145.  
  146.    ' strAttachment = strAttachment & "\\stm-fs1\finapps\dynamics\feedback\" & xlName & ".htm; "
  147.    
  148.    xlBook.Save
  149.    xlBook.Close
  150.  
  151. End If
  152.  
  153. ' New temp code - 040406
  154. ' Loop through and search each subfolder of the current folder.
  155. For Each olNewFolder In CurrentFolder.Folders
  156.  
  157. Select Case olNewFolder.Name
  158.  
  159. Case "Deleted Items", "Drafts", "Export", "Junk E - mail", "Notes"
  160. Case "Outbox", "Sent Items", "Search Folders", "Calendar", "Inbox"
  161. Case "Contacts", "Journal", "Shortcuts", "Tasks", "Folder Lists"
  162. Case Else
  163. ProcessFolder olNewFolder
  164.  
  165. End Select
  166.  
  167. Next olNewFolder
  168.  
  169. ' The next five lines are the original code
  170. ' Loop through and search each subfolder of the current folder.
  171. ' For Each olNewFolder In CurrentFolder.Folders
  172. ' If olNewFolder.Name <> "Deleted Items" And olNewFolder.Name <> "Drafts" And olNewFolder.Name <> "Export" And olNewFolder.Name <> "Junk E - mail" And olNewFolder.Name <> "Outbox" And olNewFolder.Name <> "Sent Items" And olNewFolder.Name <> "Search Folders" And olNewFolder.Name <> "Calendar" And olNewFolder.Name <> "Contacts" And olNewFolder.Name <> "Notes" And olNewFolder.Name <> "Journal" And olNewFolder.Name <> "Shortcuts" And olNewFolder.Name <> "Tasks" And olNewFolder.Name <> "Folder Lists" And olNewFolder.Name <> "Inbox" Then
  173.        
  174. ' ProcessFolder olNewFolder
  175.      
  176.       ' End If
  177.    ' Next
  178. End Sub
  179.  
  180.  
  181. Private Sub readability_and_HTML_export()
  182. '
  183. ' readability_and_HTML_export Macro
  184. ' Macro recorded 10/11/2004 by Greg Johnson
  185.  
  186. '
  187.     Cells.Select
  188.     Cells.EntireColumn.AutoFit
  189.     Cells.EntireRow.AutoFit
  190.     Columns("A:A").ColumnWidth = 32
  191. '    Range("A1").Select
  192. '    Range(Selection, Selection.End(xlDown)).Select
  193. '    Range(Selection, Selection.End(xlToRight)).Select
  194.     Cells.Select
  195.     With Selection
  196.         .HorizontalAlignment = xlGeneral
  197.         .VerticalAlignment = xlTop
  198.         .Orientation = 0
  199.         .AddIndent = False
  200.         .IndentLevel = 0
  201.         .ShrinkToFit = False
  202.         .ReadingOrder = xlContext
  203.         .MergeCells = False
  204. 'added WrapText=True 4-25-06
  205.         .WrapText = True
  206.     End With
  207.     Selection.Borders(xlDiagonalDown).LineStyle = xlNone
  208.     Selection.Borders(xlDiagonalUp).LineStyle = xlNone
  209.     Selection.Borders(xlEdgeLeft).LineStyle = xlNone
  210.     Selection.Borders(xlEdgeTop).LineStyle = xlNone
  211.     Selection.Borders(xlEdgeBottom).LineStyle = xlNone
  212.     Selection.Borders(xlEdgeRight).LineStyle = xlNone
  213.     With Selection.Borders(xlInsideVertical)
  214.         .LineStyle = xlContinuous
  215.         .Weight = xlThin
  216.         .ColorIndex = xlAutomatic
  217.     End With
  218.     With Selection.Borders(xlInsideHorizontal)
  219.         .LineStyle = xlContinuous
  220.         .Weight = xlThin
  221.         .ColorIndex = xlAutomatic
  222.     End With
  223.     Range("A1:D1").Select
  224.     With Selection.Interior
  225.         .ColorIndex = 37
  226.         .Pattern = xlSolid
  227.     End With
  228.     Selection.Font.Bold = True
  229.     Columns("C:C").Select
  230.     With Selection
  231.         .HorizontalAlignment = xlLeft
  232.         .WrapText = False
  233.         .Orientation = 0
  234.         .AddIndent = False
  235.         .IndentLevel = 0
  236.         .ShrinkToFit = False
  237.         .ReadingOrder = xlContext
  238.         .MergeCells = False
  239.     End With
  240.     If Columns("D:D").ColumnWidth < 80 Then
  241.         Columns("D:D").ColumnWidth = 80
  242.     End If
  243.  
  244.     If Columns("B:B").ColumnWidth > 40 Then
  245.         Columns("B:B").ColumnWidth = 40
  246.     End If
  247. End Sub
  248.  
  249.  
  250.  
  251. 'Private Sub DTSMailer(messagebody As String, attachmentstring As String)
  252. Private Sub DTSMailer()
  253.     Dim oPKG As New DTS.Package
  254.    
  255.     oPKG.LoadFromSQLServer "ol-dbsrvr-02", , , _
  256.         DTSSQLStgFlag_UseTrustedConnection, , , , "Feedback_Mailer"
  257.     oPKG.FailOnError = True
  258.  
  259. '    oPKG.GlobalVariables.Item("messagebody") = messagebody
  260. '    oPKG.GlobalVariables.Item("attachmentstring") = attachmentstring
  261.    
  262.     oPKG.Execute
  263.     oPKG.UnInitialize
  264.     Set oPKG = Nothing
  265. End Sub