Hello VBA experts,
I have a spreadsheet that I would like to add a command button to that would populate an email to the corresponding email address, but only if a condition is met (an upcoming due date). I have tried several versions of code, but none have worked so far and I'm also a beginner VBA user so I haven't been able to try anything too complicated.
Here is how my spreadsheet is set up: I have a due date in Column G and a drop down list of names in Column I, which populates the email address for that person into Column L. What I want to do is add a command button which, when clicked, will display an email to the person's email address in Column L, but only if the due date in Column G is coming up within the week, or is past due. I need the macro to check each row for the due date and email address, then move on to the next row. I did try creating a "helper" Column J that says "email" if the due date is <=TODAY() +7, but it's just not working. I don't know if it's worth noting, but there are three worksheets in my workbook, but this button and macro only needs to apply to one.
I tried using the follow code which I found on Ron de Bruin's excel automation site, but it isn't working, or giving me any errors, so I'm not sure where the problem is. Any help would be greatly appreciated. I hope that all makes sense.
Sub DisplayEmail()
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
On Error GoTo cleanup
For Each cell In Columns("L").Cells.SpecialCells(xlCellTypeConstants)
If cell.Value Like "?*@?*.?*" And _
LCase(Cells(cell.Row, "J").Value) = "email" Then
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = cell.Value
.Subject = "Action Item Due"
.Body = "Hi there, " & vbNewLine & vbNewLine & _
"You have an action item with an upcoming due date."
'You can add files also like this
'.Attachments.Add ("C:\test.txt")
.Display 'Or use Display
End With
On Error GoTo 0
Set OutMail = Nothing
End If
Next cell
cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
End Sub
I have a spreadsheet that I would like to add a command button to that would populate an email to the corresponding email address, but only if a condition is met (an upcoming due date). I have tried several versions of code, but none have worked so far and I'm also a beginner VBA user so I haven't been able to try anything too complicated.
Here is how my spreadsheet is set up: I have a due date in Column G and a drop down list of names in Column I, which populates the email address for that person into Column L. What I want to do is add a command button which, when clicked, will display an email to the person's email address in Column L, but only if the due date in Column G is coming up within the week, or is past due. I need the macro to check each row for the due date and email address, then move on to the next row. I did try creating a "helper" Column J that says "email" if the due date is <=TODAY() +7, but it's just not working. I don't know if it's worth noting, but there are three worksheets in my workbook, but this button and macro only needs to apply to one.
I tried using the follow code which I found on Ron de Bruin's excel automation site, but it isn't working, or giving me any errors, so I'm not sure where the problem is. Any help would be greatly appreciated. I hope that all makes sense.
Sub DisplayEmail()
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
On Error GoTo cleanup
For Each cell In Columns("L").Cells.SpecialCells(xlCellTypeConstants)
If cell.Value Like "?*@?*.?*" And _
LCase(Cells(cell.Row, "J").Value) = "email" Then
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = cell.Value
.Subject = "Action Item Due"
.Body = "Hi there, " & vbNewLine & vbNewLine & _
"You have an action item with an upcoming due date."
'You can add files also like this
'.Attachments.Add ("C:\test.txt")
.Display 'Or use Display
End With
On Error GoTo 0
Set OutMail = Nothing
End If
Next cell
cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
End Sub