Hi All, I have some code which does a great job (mostly) of sending out monthly statements of which I have ~300. However I get runtime error 440 after it generates about 30 emails (different number each time) and I therefore have to send them out in batches. When I click debug it is always the ".To" line which is the problem, but there is no issue with the email addresses listed and as the number of emails it generates before the error varies I cant attribute the issue to any particular line or email address. Here is my code, is anyone able to assist please?
It is probably worth noting that for most of the emails the statements are just copied and pasted into the email body but where column M is equal to "1234" then an excel email attachment of the budget is also sent. There isnt a problem with this element of the code but I just mention it incase you wonder what's going on.
It is probably worth noting that for most of the emails the statements are just copied and pasted into the email body but where column M is equal to "1234" then an excel email attachment of the budget is also sent. There isnt a problem with this element of the code but I just mention it incase you wonder what's going on.
VBA Code:
Sub runmacro()
Dim response As VbMsgBoxResult
'You need to unlock the statements in cell D1
If Sheets("01. STATEMENT").Range("D1").Value <> "SEND EMAILS UNLOCKED" Then
response = MsgBox("Cell D1 must be set to 'SEND EMAILS UNLOCKED' to continue", vbOKOnly)
If response = vbOK Then Exit Sub
End If
'2 message boxes to give you a chance to cancel before sending out the emails
response = MsgBox("You are about to send ~400 email statements, do you want to continue??", vbOKCancel + vbInformation)
If response = vbCancel Then Exit Sub
response = MsgBox("ARE YOU SURE??", vbOKCancel + vbInformation)
If response = vbCancel Then Exit Sub
'Filter only on the budget categories with either expenditure or budget or both
ActiveSheet.ShowAllData
ActiveSheet.Range("$G$1:$R$5001").AutoFilter Field:=1, Criteria1:="YES"
'Set statements back to locked so they are locked next time you open the spreadsheet
Range("D1").Value = "SEND EMAILS LOCKED"
'Loop through the macro for all the seperate statements:
Dim n As Long
For n = 7 To 4997 Step 10
Mail_Selection_Range_Outlook_Body CStr(n)
Next n
End Sub
Sub Mail_Selection_Range_Outlook_Body(ByVal row As String)
'If the statement is active then continue or skip
If Sheets("01. STATEMENT").Range("H" & row).Value = "YES" Then
Dim OutApp1 As Object
Dim OutMail1 As Object
Dim wdDoc1 As Object
Dim oRng1 As Object
Set OutApp1 = CreateObject("Outlook.Application")
Set OutMail1 = OutApp1.CreateItem(0)
If Sheets("01. STATEMENT").Range("m" & row).Value = "1234" Then
'Set the name and file location of the excel attachment
Excelfile = ActiveWorkbook.FullName
i = InStrRev(Excelfile, ".")
If i > 1 Then Excelfile = Left(Excelfile, i - 1)
Excelfile = Environ("USERPROFILE") & "\OneDrive - x\Documents\" & Sheet1.Range("J" & row) & " Financial Report " & Format(Sheet1.Range("C1"), "mmm.yy") & ".xlsx"
'Create the excel attachment
Range("A" & row - 5 & ":" & "E" & row + 4).Select
Selection.Copy
Workbooks.Add
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteFormats
Selection.PasteSpecial Paste:=xlPasteColumnWidths
Selection.PasteSpecial Paste:=xlPasteValues
ActiveWorkbook.SaveAs Filename:=Excelfile
ActiveWorkbook.Close
End If
'Copy the statement to post into the email body
Workbooks("x statements LIVE.xlsm").Activate
Sheets("01. STATEMENT").Range("A" & row - 5 & ":" & "E" & row + 4).Select
Selection.Copy
'Create the email
With OutMail1
.Display
.To = Sheets("01. STATEMENT").Range("O" & row)
.Cc = Sheets("01. STATEMENT").Range("R" & row)
.Subject = Sheets("01. STATEMENT").Range("J" & row) & " " & Sheets("01. STATEMENT").Range("K" & row) & "Financial Report " & Sheets("01. STATEMENT").Range("C1")
.Body = ""
If Sheets("01. STATEMENT").Range("m" & row).Value = "1234" Then
.Attachments.Add Excelfile
'Delete the excel file from one drive
Kill (Excelfile)
End If
'Email body AFTER statement
.htmlbody = "This email account is not monitored"
Set olInsp = .GetInspector
Set wdDoc1 = olInsp.WordEditor
Set oRng1 = wdDoc1.Range
oRng1.collapse 1
oRng1.Paste
For Each shp In wdDoc1.InlineShapes
shp.ScaleHeight = 110
shp.ScaleWidth = 110
Next
'Email body BEFORE statement
.htmlbody = "<font style='font-family:calibri;font-size:15.0'>" & "Dear " & Sheet1.Range("N" & row) & "," & "<br>" & "<br>" _
& Sheets("01. STATEMENT").Range("K" & row) & " / Please find the financial report for the " & Sheets("01. STATEMENT").Range("K" & row) & " project below." & "<br>" & "<br>" & .htmlbody
End With
On Error GoTo 0
Set OutMail1 = Nothing
Set OutApp1 = Nothing
End If
End Sub