0

I am trying to send out emails based on a due date on my excel sheet. I have a list of items in which each item has a specific owner, the description of that item and a due date for that item.

The recepients of the item are in column "F" and the due date is in column "R". Here is the code that I have so far but I am getting an error stating that there is a Runtime error 13 and Type Mismatch. The code runs fine for a little while and then I start receiving this error. When I have multiple due dates, that is when this error occurs. I am not sure what I am doing wrong. If there is any way I can edit the code please propose it, or if there is another way about sending emails out based on a due date, please let me know the code. I will specify where in the code there is an error.

Thank you!

  Public Sub CheckAndSendMail()
 Dim lRow        As Long
 Dim lstRow      As Long
 Dim toDate      As Date
 Dim toList      As String
 Dim ccList      As String
 Dim bccList     As String
 Dim eSubject    As String
 Dim EBody       As String
 Dim vbCrLf      As String

 Dim ws          As Worksheet

 With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .DisplayAlerts = False


 End With

 Set ws = Sheets(1)
 ws.Select

 lstRow = WorksheetFunction.Max(3, ws.Cells(Rows.Count, "R").End(xlUp).Row)


 For lRow = 3 To lstRow

 'THIS IS WHERE I RECEIVE THE ERROR:
    toDate = Cells(lRow, "R").Value 

    'toDate = Replace(Cells(lRow, "L"), ".", "/")
    If Left(Cells(lRow, "R"), 17) <> "Mail" And toDate - Date <= 7 Then
   vbCrLf = "<br><br>"

        toList = Cells(lRow, "F") 'gets the recipient from col F
        eSubject = "Text" & Cells(lRow, "C") & " is due on " & Cells(lRow, "R").Value
        EBody = "<HTML><BODY>"
        EBody = EBody & "Dear " & Cells(lRow, "F").Value & vbCrLf
        EBody = EBody & "Text" & Cells(lRow, "C").Value & vbCrLf
        EBody = EBody & "Text" & vbCrLf
        EBody = EBody & "Link to the Document:"
        EBody = EBody & "<A href='Link to Document'>Text </A>"
        EBody = EBody & "</BODY></HTML>"

     Cells(lRow, "W") = "Mail Sent " & Date + Time 'Marks the row as "email sent in Column W"

        MailData msgSubject:=eSubject, msgBody:=EBody, Sendto:=toList


    End If
 Next lRow

 ActiveWorkbook.Save

 With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .DisplayAlerts = True

 End With

 End Sub



 Function MailData(msgSubject As String, msgBody As String, Sendto As String, _
    Optional CCto As String, Optional BCCto As String, Optional fAttach As String)

 Dim app As Object, Itm As Variant
 Set app = CreateObject("Outlook.Application")
 Set Itm = app.CreateItem(0)
 With Itm
    .Subject = msgSubject
    .To = Sendto
    If Not IsMissing(CCto) Then .Cc = CCto
    If Len(Trim(BCCto)) > 0 Then
        .Bcc = BCCto
    End If
    .HTMLBody = msgBody
    .BodyFormat = 2 '1=Plain text, 2=HTML 3=RichText -- ISSUE: this does not keep HTML formatting -- converts all text
    'On Error Resume Next
    If Len(Trim(fAttach)) > 0 Then .Attachments.Add (fAttach) ' Must be complete path'and filename if you require an attachment to be included
    'Err.Clear
    'On Error GoTo 0
    .Save           ' This property is used when you want to saves mail to the Concept folder
    .Display      ' This property is used when you want to display before sending
    '.Send         ' This property is used if you want to send without verification
End With
Set app = Nothing
Set Itm = Nothing
End Function

Here is the error I receive:

Error Message

adit123
  • 117
  • 2
  • 20

1 Answers1

0

Try to format the value of column R as Date before assigning to toDate. try this line of code:

toDate = CDate(Cells(lRow, "R").Value)

Also, Have you checked for the data when Cells(lRow, "R").Value returns null or empty value. This can also be the reason for the error.

Paresh J
  • 2,401
  • 3
  • 24
  • 31
  • Yes I made sure the format was the same. the column "R" is a calculated value and it returns similar formatting of the date. Also I just tried this code with the multiple dates and I still receive this error. What am I doing wrong? – adit123 Nov 11 '14 at 14:17
  • It is this.. I am not sure what I am doing wrong. I will post in my question. – adit123 Nov 11 '14 at 14:20
  • Are you sure that format of toDate is same as that of Date while subracting? Try to debug it and check for some values. Also, check for the R value that is getting assigned to tDate. – Paresh J Nov 11 '14 at 14:23
  • Yes I am sure, here is the calculated formula that spits out the date based on a selection from another column. =EDATE($Q3,LOOKUP($P3,{"Annually","Bi-Annually","Quarterly","Semi-Annually"},{12,24,3,6})) This formula makes sure that it is in date format in column "R". – adit123 Nov 11 '14 at 14:33
  • Going by the error, i.e type mismatch. It is pretty clear that error is due to inappropriate value assignment to the variable. Just check column "R" thoroughly for any inappropriate value. – Paresh J Nov 11 '14 at 14:38
  • OKay so I cleared all of the contents in column "R". and I tried to play around with three dates with the same format. So the code works fine with only two dates. No error , whereit says Type Mismatch comes up. However when I add a third date, with the same format with the other two. the error comes up. I am not sure what I am doing wrong. I made sure everything in column "R" was first clear and I made sure that they are all in Date format... "mm/dd/yyyy" . Could you try testing it? I honestly do not know where I am going wrong. I made sure everything is in the same format. – adit123 Nov 11 '14 at 15:04
  • At the time of subraction, use this code: Format(toDate, "mm/dd/yyyy") - Format(Date, "mm/dd/yyyy") <= 7 – Paresh J Nov 11 '14 at 15:10
  • I tried that code and I am still receiving the mismatch code. This time where I added your code to the Subtraction. with the same criteria. I am just entering only two dates and running the macro and this time an email will not even display. Is there something wrong with my settings or what? I made sure that I formatted all of Column "R" into the Date format ("mm/dd/yyyy"). – adit123 Nov 11 '14 at 15:18
  • For testing purpose, just check this code: DateAdd("d", 5, Date) - Date <= 7. I am sure it should not give any error. – Paresh J Nov 11 '14 at 15:25
  • I did receive the same error :( . Again with two dates it the code was working fine but then when I add a third date I get this code. I am so confused. And I have over 500 items and I would need to get this code working for more than two dates. Would you like me to send you a sample of the excel sheet with the code so you can test it Paresh? – adit123 Nov 11 '14 at 15:46
  • Just check the format of the third date then. And can you post excel file here? – Paresh J Nov 11 '14 at 15:48
  • I did check the format of the third date and also I created a sample excel sheet and edited my current code to correlate with the excel sheet. Also, how can I attach the excel sheet? I tried to attache it to my question but there is no option for attaching. How should i send this code. and I truly appreciate the help! – adit123 Nov 11 '14 at 16:17