Hey There,
I am brand new to this forum but I was wondering if anyone can help. I keep receiving an Invalid Next control variable reference. I'm trying to completely automate an email system, I'm one final step away! I've attached my code, I keep receiving the error at the "Next Fnum" stage at the very end of the code..
Sub SendEmail()
Dim MyPath As String, FilesInPath As String
Dim MyFiles() As String, Fnum As Long
Dim mybook As Workbook
Dim CalcMode As Long
Dim sh As Worksheet
Dim ErrorYes As Boolean
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Dim Addresslist As Scripting.Dictionary
'Fill in the path\folder where the files are
MyPath = "C:\Users\User\Documents\Expediting\Expediting Reports"
'Add a slash at the end if the user forget it
If Right(MyPath, 1) <> "\" Then
MyPath = MyPath & "\"
End If
'If there are no Excel files in the folder exit the sub
FilesInPath = Dir(MyPath & "*.xl*")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If
'Fill the array(myFiles)with the list of Excel files in the folder
Fnum = 0
Do While FilesInPath <> ""
Fnum = Fnum + 1
ReDim Preserve MyFiles(1 To Fnum)
MyFiles(Fnum) = FilesInPath
FilesInPath = Dir()
Loop
'Change ScreenUpdating, Calculation and EnableEvents
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
Set OutApp = CreateObject("Outlook.Application")
'Loop through all files in the array(myFiles)
If Fnum > 0 Then
For Fnum = LBound(MyFiles) To UBound(MyFiles)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(MyPath & MyFiles(Fnum))
On Error GoTo 0
If Not mybook Is Nothing Then
If mybook.Worksheets(1).Range("OP2").Value <> "" Then
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
For Each cell In Columns("R").Cells.SpecialCells(xlCellTypeConstants)
If cell.Value Like "?*@?*.?*" Then
On Error Resume Next
Addresslist.Add cell.Value, cell.Value
If Err.Number = 0 Then
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = mybook.Worksheets(1).Range("P2").Value
.CC = cell.Value
.Subject = "Mosaic PO Expediting Report - Overdue Purchase Orders " & Format(Date, "dd/mm/yy")
.Body = "Good Day, " & vbLf _
& vbLf _
& "Please review the attached file"
.Attachments.Add mybook.FullName
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
.Display 'change to .Send if it works
End With
Set OutMail = Nothing
If Err.Number > 0 Then
ErrorYes = True
Err.Clear
On Error GoTo 0
End If
Else
ErrorYes = True
End If
'Save and close mybook
mybook.Close savechanges:=False
Else
'Not possible to open the workbook
ErrorYes = True
End If
Next Fnum
End If
If ErrorYes = True Then
MsgBox "There are problems in one or more files, possible problem:" _
& vbNewLine & "???????????????"
End If
Set OutApp = Nothing
'Restore ScreenUpdating, Calculation and EnableEvents
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
On Error Resume Next
Kill "C:\Users\dslack\Documents\Expediting\Expediting Reports\*.xls"
On Error GoTo 0
End Sub
Bookmarks