Home > OS >  Automating Outgoing Emails Issue
Automating Outgoing Emails Issue

Time:04-27

I am new to this, so please bear with me. Thank you in advance for your help. I am writing code for many emails that I send out quarterly and monthly for my job to automate them, so I don't have to keep sending them out.

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 str in the vba code, make it recurring and set a reminder for the future.
  • My code is stored in Microsoft outlook objects "thisoutlooksession"

My code does this:

  1. Sets Application_Reminder as an object
  2. Sets DIM as mailItem and strbody(for body of string)
  3. If then statement Item.Class = OlTask Then
  4. look in substring for item with a certain str 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 flawlessly.

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 properly 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.

Here is my code (Please note I deleted all sensitive info, there is 11 emails but I only show 2 here 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

CodePudding user response:

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.

  • Related