Search code examples
vbaoutlook

Automating Outgoing Emails will not send any emails after working yesterday


I am writing code for emails I send quarterly and monthly.

My Problem: It will not send any emails after I got it working yesterday.

Steps:

  • Setup a task with a subject line that will match the string in the VBA code, make it recurring and set a reminder for the future.
  • My code is in "ThisIOutlookSession".

My code:

  1. Sets Application_Reminder as an object
  2. Sets DIM as mailItem and strbody(for body of string)
  3. If Item.Class = OlTask Then
  4. look in substring for item with subject "api movements recurring email" Then
  5. If true uses the variable to create an email
  6. strbody of the email
  7. WITH statements to include subject, to addresses, HTMLbody, and attachments.
  8. sends the email
  9. many ElseIf statements to include multiple emails.

Yesterday I had the monthly emails set up to run, and they did.

I think the issue is: The tasks being created, or perhaps it is the application reminder object. Also, I wonder if olTask is not appropriate for recurring tasks for this to run because perhaps the tasks change after their due date. I also tried completing the tasks and then sending the reoccurrence, but that didn't work either.

My code (I deleted sensitive information. There are 11 emails but I only show two for the sake of reviewing it all.):

Private Sub Application_Reminder(ByVal Item As Object)
    Dim objPeriodicalMail As MailItem
    Dim strbody As String
    
    If Item.Class = olTask Then
    'change the following item subject so it knows what to send
       If InStr(LCase(Item.Subject), "api movements recurring email") Then
          Set objPeriodicalMail = Outlook.Application.CreateItem(olMailItem)
          'Change the following email information as per your actual needs
          strbody = "<H3><B>Hi Sanmin,</B></H3>" & _
                    "I hope this e-mail finds you well.  This is a friendly reminder to please send the completed API material movements checklist for this month by the first business day of the coming month. I have attached the month-end checklist for your use. If there are no API movements in transit at the end of the month, please send me an e-mail stating so." & _
                    "<br><br><B>Thank you,</B>" & _
                    "<br><br><br><B></B>" & _
                    "<br><B></B>" & _
                    "<br><B></B>" & _
                    "<br><B></B>" & _
                    "<br><B></B>" & _
                    "<br><B>P : </B>" & _
                    "<br><B>E: </B>"

          
          With objPeriodicalMail
               .Subject = "API Movements Report"
               .To = ""
               '.To = ""
               '.CC = ""
               .HTMLBody = strbody & .HTMLBody
               .Attachments.Add "C:\Automated Emails\API In-Transit Material Movements Checklist.xlsx"
               .Send
          End With

          
        'change the following item subject so it knows what to send
       ElseIf InStr(LCase(Item.Subject), "beth quarterly recurring email") Then
          Set objPeriodicalMail = Outlook.Application.CreateItem(olMailItem)
          'Change the following email information as per your actual needs
            strbody = "<H3><B>Hi Beth,</B></H3>" & _
                    "" & _
                    "<br><br><B>Thank you,</B>" & _
                    "<br><br><br><B></B>" & _
                    "<br><B></B>" & _
                    "<br><B></B>" & _
                    "<br><B></B>" & _
                    "<br><B></B>" & _
                    "<br><B></B>" & _
                    "<br><B></B>"

          
          With objPeriodicalMail
               .Subject = "Quarterly Inventory Reserve Inquiry"
               .To = ""
               '.To = ""
               '.CC = ""
               .HTMLBody = strbody & .HTMLBody
               .Send
          End With
       End If
    End If
End Sub

Solution

  • Your code is valid, I don't see any possible causes that could stop it from running. You can set a breakpoint and wait until the reminder event is fired, so you will be able to debug the code and see how it is working.

    But VBA macros can be disabled in Outlook and your code may never run. I'd suggest checking the Trust Center settings in Outlook to make sure VBA macros are not disabled.

    Another possible alternative is timers. See Outlook VBA - Run a code every half an hour for more information.