2

I Believe this question is very common but I am getting a very unique situation.

I have a code that I am using for delay delivery. But the problem is I am unable to run the macro.

Public Sub Applicaion_Reminder(ByVal Item As Object)
Dim objPeriodicalMail As MailItem
 
    If Item.Class = olTask Then
       If InStr(LCase(Item.Subject), "send an email periodically") Then
          Set objPeriodicalMail = Outlook.Application.CreateItem(olMailItem)
          'Change the following email information as per your actual needs
          With objPeriodicalMail
               .Subject = "Email to Gmail"
               .To = "bfarhan8@gmail.com"
               .HTMLBody = "<HTML><BODY>It's a Test</HTML></BODY>"
               
               .Importance = olImportanceHigh
               .ReadReceiptRequested = True
               .Send
          End With
       End If
    End If
End Sub

When I hit on the run it asks me for Macro Name when I define a name it creates a new Sub.

Image Reference

If I remove the parameters of the

Application_Reminder() to match the name with a macro name It gives an error on line number 3. Error Reference

My question is how to run this Macro Properly. I searched the web but didn't find any useful help.

Uniquedesign
  • 423
  • 1
  • 7
  • 23
  • I followed the requirements from this link https://www.datanumen.com/blogs/auto-send-recurring-email-periodically-outlook-vba/. – Farhan Basheer. Jul 13 '20 at 07:46
  • 1
    Here is the instruction that you did not follow: `copy the following VBA codes into the “ThisOutlookSession” project.` – braX Jul 13 '20 at 07:59
  • I did follow everything. – Farhan Basheer. Jul 13 '20 at 08:17
  • Your screenshot clearly shows otherwise. You put it in a module instead. – braX Jul 13 '20 at 08:35
  • 1
    See the little tree in the top left? see how one item is called "ThisOutlookSession" and the other item is called "Module1"? You put it in the wrong place. – braX Jul 13 '20 at 08:36
  • 1
    You shouldn't get defensive, we all get thing wrong sometimes. @braX is absolutely right and he is only trying to help you. You need to double-click on "ThisOutlookSession" and paste the code there. – Super Symmetry Jul 13 '20 at 10:31
  • First of all Thank you so very much for your help second I am not getting defensive, you don't know how much these suggestions are helpful for me. Now i double clicked on the the outlook session and when editor pop upped i pasted the code there when i clicked on the run button it asked for macro name. i will try again and lets see what will happen. – Farhan Basheer. Jul 13 '20 at 18:32

2 Answers2

2

What you have missed is that macros can either be subroutines or functions and for each there are two major types.

A subroutine does something. Your Application_Reminder is a subroutine because it does something: send a reminder. A function can do something, but its real purpose is to return a value.

Some subroutines and functions need parameters, but some do not.

If I write a function Sqrt, the immediate question is: square root of what? I want to be able to write:

Answer = Sqrt(5)

That is, today I want the square root of 5. Tomorrow, I might want the square root of 7.

I would write:

 Function Sqrt(ByVal Number as Double) as Double
 ‘ Code to calculate square root of Number
 Sqrt = ResultOfCalculation
End Function

Almost all functions have parameters, but it is not essential. I could have a function, GetCurrentTemperature that reads a thermometer and returns a temperature. It would not need a parameter.

You have written a subroutine that has a parameter: Applicaion_Reminder(ByVal Item As Object). When you try to run Applicaion_Reminder, the interpreter wants to know what Item. I do not think the interpreter’s response is very sensible. It should have just told you, “You cannot run a subroutine with a parameter.”

You need a subroutine without a parameter that decides which Item is to be processed. With some computer languages, that subroutine must have the name Main. With VBA it can have any name.

That is, you need a subroutine like this:

Sub PickAnItemThatNeedsAReminder()
  Dim Item as Object
  ‘ Code to set Item to the required MailItem
 Call Applicaion_Reminder(Item)
End Sub  

There are four distinct methods of selecting a MailItem. I imagine you scrolling down your Sent Items folder looking for emails to which you have not received a reply. When you find such an email, you run PickAnItemThatNeedsAReminderwhich sends a reminder.

Sub PickAnItemThatNeedsAReminder ()

  Dim Exp As Explorer
  Dim Item As Object

  Set Exp = Outlook.Application.ActiveExplorer
  
  If Exp.Selection.Count = 0 Then
    Call MsgBox("Please select one or more emails then try again", vbOKOnly)
    Exit Sub
  Else
    For Each Item In Exp.Selection
      Call Applicaion_Reminder(Item)
    Next
  End If

End Sub

Exp.Selection is a list of all the currently selected emails. You can select as many emails as you want and them run PickAnItemThatNeedsAReminder. It will call Applicaion_Reminder for every selected email.

Additional Background

My belief is you have found a routine that runs off an event and have tried to adapt it to your requirements. Events are an incredibly useful feature of Outlook. However, if you do not yet understand that you cannot run a macro without a parameter, you are not yet ready for events. We say: walk before you run.

BraX and Super Symmetry would be correct in telling you to use ThisOutlookSession if you are going to use events. I have suggested you use Explorer (which is technically an event) but which is much easier for a beginner to understand than an application level event which is what you seemed to have found. With my approach, all your code can be in an ordinary module.

Tony Dallimore
  • 12,335
  • 7
  • 32
  • 61
  • Thanks for the explanation.Actually i want to send an email at specific time which i have defined in the "tasks". I have tried your code but it's not responding as i need. What i want to do is, first of all i want to sent an email at 7PM today then after that i want to send an email at 8 AM Tomorrow and same for other days, I have a script which is working very well for 7 PM but this script to send an email at 8 AM Everyday is not working. Any guess what i am missing? – Farhan Basheer. Jul 15 '20 at 05:36
  • 1
    One of the objectives of this site is to build up a database of questions and answers that can be indexed. Subsidiary questions are discouraged since they are difficult to index. A question about sending an email at a specific time is a new question that does not relate to the current question. You should accept an answer if it has been helpful, look for questions about sending an email at a specific time and, if necessary, post a new question with the code that does not work as you want. – Tony Dallimore Jul 15 '20 at 13:31
0

Application.Reminder event Occurs immediately before a reminder is displayed set up Task Item with reminder then call your vba function - Applicaion_Reminder

See example on this answer

https://stackoverflow.com/a/40144594/4539709

if you want to call it with selected email then see Tony's answer

0m3r
  • 12,286
  • 15
  • 35
  • 71