Hi All,
I am new to VBA and trying to learn the nitty-gritty of excel vba. My query is that I am trying to send an email automatically from my excel sheet through outlook 2007 application. It is a simple macro which checks for the condition for the first cell in row while using two other cell in the same row as email id and message bidy. The issue that I am facing is the loop which according to me is pretty simple but still it is giving the error "The object has been moved/deleted" while executing and sending the email successfully for the first cell in the range (Cells_M).
Sub SendMail()
Dim olApp As Outlook.Application
Dim olMail As MailItem
Dim Cell As Range
Dim Cells_M As Range
Set olApp = New Outlook.Application
Set olMail = olApp.CreateItem(olMailItem)
ActiveWorkbook.Save
Set Cells_M = Range("A4:A6")
For Each Cell In Cells_M
If Cell.Value > Date Then
With olMail
.To = Cell.Offset(0, 4).Value 'I have tried Cell.Offset(0, 4).text also but same error". This cell has the email id
.Subject = "Pending Reports"
.Body = Cell.Offset(0, 1).Text 'This cell has text or the report which is pending
.Send
End With
Else
End If
Next Cell
Set olMail = Nothing
Set olApp = Nothing
End Sub
Bookmarks