• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Out of memory or system resources. Close some windows or programs and try again

ThrottleWorks

Excel Ninja
Hi,

I am creating email by below mentioned code. This code saves email as draft in outlook.

At a single run macro is supposed to save more than 250 emails. However somewhere around 50 emails.

I am facing below bug.
Out of memory or system resources. Close some windows or programs and try again

Can anyone please help me to resolve this.

Code used for creating e-mails.
Code:
Option Explicit
Sub Create_Email()
    Dim EmailRng As Range
    Dim OutApp As Object
    Dim OutMail As Object
   
    EmailSht.Range("B4") = Application.UserName
    EmailSht.Range("B5").FormulaR1C1 = "=SEARCH("","",R[-1]C)+2"
    EmailSht.Range("B6").FormulaR1C1 = "=SEARCH(""("",R[-2]C)"
    EmailSht.Range("B7").FormulaR1C1 = "=R[-1]C-R[-2]C"
    EmailSht.Range("B8").FormulaR1C1 = "=MID(R[-4]C,R[-3]C,R[-1]C)"
    EmailSht.Range("B9").FormulaR1C1 = "=LEFT(R[-5]C,R[-4]C-3)"
    EmailSht.Range("B10").FormulaR1C1 = "=R[-2]C&"" ""&R[-1]C"
    EmailSht.Range("B5:B10").Value = EmailSht.Range("B5:B10").Value
   
    Dim TempLr As Long
    TempLr = EmailSht.Cells(EmailSht.Rows.Count, 5).End(xlUp).Row
    Set EmailRng = EmailSht.Range("F1:F" & TempLr)
   
    MacroBook.Activate
    EmailSht.Select
    EmailSht.Range("A1").Select
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    Dim MyPath As String
    MyPath = ThisWorkbook.Worksheets("Mapping").Range("A2").Value

    With OutMail
        .To = EmailSht.Range("B1").Value
        .CC = EmailSht.Range("B2").Value
        '.BCC = EmailMapSht.Range("B1").Value
        .Subject = EmailSht.Range("B3").Value
        .Attachments.Add (MapSht.Range("A3").Value)
        .HTMLBody = RangetoHTML(EmailRng)
        .Display
    End With

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub
Function RangetoHTML(rng As Range)
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook
   
    Dim MacroBook As Workbook
    Dim EmailSht As Worksheet
   
    Set MacroBook = ThisWorkbook
    Set EmailSht = MacroBook.Worksheets(Email_Body.Name)
   
    TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
   
    Dim TempLr As Long
    Dim EmailRng As Range
   
    TempLr = EmailSht.Cells(EmailSht.Rows.Count, 6).End(xlUp).Row
    Set EmailRng = EmailSht.Range("F1:F" & TempLr)
   
    EmailRng.Copy
    Set TempWB = Workbooks.Add(1)
    TempWB.Worksheets("Sheet1").Range("A1").PasteSpecial xlPasteValues
    TempWB.Worksheets("Sheet1").Range("A1").PasteSpecial xlPasteFormats
   
    Columns("A:A").ColumnWidth = 150
    Cells.Font.Name = "Calibri"
    Cells.Font.Size = 10
    Cells.Font.Color = 8210719
    Columns.AutoFit
   
    With TempWB.Sheets(1)
        On Error Resume Next
            .DrawingObjects.Visible = True
            .DrawingObjects.Delete
        On Error GoTo 0
    End With
   
    With TempWB.PublishObjects.Add( _
        SourceType:=xlSourceRange, Filename:=TempFile, Sheet:=TempWB.Sheets(1).Name, _
        Source:=TempWB.Sheets(1).UsedRange.Address, HtmlType:=xlHtmlStatic)
        .Publish (True)
    End With

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
   
    RangetoHTML = ts.readall
    ts.Close
   
    RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", "align=left x:publishsource=")
    RangetoHTML = Replace(RangetoHTML, "<!--[if !excel]>&nbsp;&nbsp;<![endif]-->", "")
    TempWB.Close savechanges:=False
   
    Kill TempFile

    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function
 
Hi @SirJB7 , sorry for late reply. Bug is at .display line.

My apology, missed out mentioning in first post.
Email creation module is called from a loop. Please see below loop code for reference.

Loop code
Code:
For Each SRng In SearchRng
        EmailSht.Range("B1").Value = SRng.Offset(0, 1)
      
        TempCash.AutoFilterMode = False
        CashRng.AutoFilter Field:=7, Criteria1:="=" & SRng, Operator:=xlAnd
      
        TempLr = TempCash.Cells(TempCash.Rows.Count, 1).End(xlUp).Row
        TempCol.Cells.Clear
        If TempLr <> 1 Then
            Set TempRng = TempCash.Range(TempCash.Cells(1, 1), TempCash.Cells(TempLr, 6))
            TempRng.SpecialCells(xlCellTypeVisible).Copy
            TempCol.Range("A1").PasteSpecial xlPasteAll
        End If
      
        TempStock.AutoFilterMode = False
        StockRng.AutoFilter Field:=7, Criteria1:="=" & SRng, Operator:=xlAnd
      
        TempLr = TempStock.Cells(TempStock.Rows.Count, 1).End(xlUp).Row
        If TempLr <> 1 Then
            Set TempRng = TempStock.Range(TempStock.Cells(1, 1), TempStock.Cells(TempLr, 6))
            TempRng.SpecialCells(xlCellTypeVisible).Copy
          
            TempLr = TempCol.Cells(TempCol.Rows.Count, 1).End(xlUp).Row + 4
            TempCol.Cells(TempLr, 1).PasteSpecial xlPasteAll
        End If
      
        TempCol.Columns.AutoFit
        TempLr = TempCol.Cells(TempCol.Rows.Count, 1).End(xlUp).Row
        If TempLr <> 1 Then
            TempCol.Cells.Copy
            Workbooks.Add
            Range("A1").PasteSpecial xlPasteAll
          
            Call SaveMyFile
            Call Create_Email
        End If
    Next SRng

Call SaveMyFile code is as below.
Code:
Sub SaveMyFile()
    Dim MyPath As String
    Dim MyReportName As String
  
    MyPath = MapSht.Range("A2").Value
    MapSht.Range("A4") = TempCol.Range("E2") & "-" & TempCol.Range("F2") & " " & MacroSht.Range("G11").Value
  
    On Error Resume Next
        MapSht.Range("A4:A5").Replace What:="/", Replacement:=" ", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    On Error GoTo 0
  
    MyReportName = MapSht.Range("A4")
    EmailSht.Range("B3").Value = TempCol.Range("E2") & "-" & TempCol.Range("F2") & " " & MacroSht.Range("G11").Value & " Dummy"
  
    ActiveWorkbook.SaveAs Filename:=MyPath & "\" & MyReportName & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    ActiveWorkbook.Worksheets("Sheet2").Delete
    ActiveWorkbook.Worksheets("Sheet3").Delete
    MapSht.Range("A3") = ActiveWorkbook.FullName
    ActiveWorkbook.Save
    ActiveWorkbook.Close
End Sub
 
Hmm, my guess. Is that since you are carrying over object variable across multiple subs/functions. It isn't being released properly and eating up memory.

Edit: Or that ".displayed" email window isn't being closed and hogging memory.
 
Hmm, my guess. Is that since you are carrying over object variable across multiple subs/functions. It isn't being released properly and eating up memory.

Edit: Or that ".displayed" email window isn't being closed and hogging memory.

Agree! Next instance of mail should be created after closing/sending the 1st else it will occupy lots of memory as loop is in the queue there.
 
Hi @Chihiro sir, you are right. Thanks for the help. Have a nice day ahead. :)

Hi @Deepak sir, agree with you, however can no use .send. Tried .close yesterday only,but it is not working.

Can you please tell me, how to close created e-mail.
 
Why .send not working. Have u checked you email address, is this correct.

Limit loop to 2.
Put your own email in to/cc.

Using smtp or exchange?
 
Hi @Deepak sir, can not use .send. E-mail will be sent after manual review hence can not use .send. Please give me some time to check macro with lesser items.

Thanks for the help. :)
 
Hi @Chihiro sir and @Deepak sir, thanks a lot. It is working nice. :)

Have a nice day ahead.

PS - However, to be honest, I did not understand what is OlSave.
I saved this variable as variant for the time being.
 
@Deepak sir, awesome. Thanks.

Though not an issue for me, just as FYI (for Forum). This method takes much more time than regular .Display method. So I guess, should be used only if required (like in my case).

Good night. :)
 
Back
Top