I am trying to send emails with a five second interval.
I am using the Kernel32 Sleep function to insert a delay in my code. The problem is my email sending loop processes all the Sleep timers and then sends a batch of emails together.
I used the message box to confirm this. I think it might be because of multi threading but I am lost as how to make this an atomic function.
Here is a snippet of my code:
Public Declare PtrSafe Sub Sleep Lib "Kernel32" (ByVal dwMilliseconds As LongPtr)
Sub Send_Emails()
Dim i As Integer
For i = 1 To 4
Sleep (5000)
Dim OutlookApp As Outlook.Application
Dim OutlookMail As Outlook.MailItem
Set OutlookApp = New Outlook.Application
Set OutlookMail = OutlookApp.CreateItem(olMailItem)
With OutlookMail
.BodyFormat = olFormatHTML
.HTMLBody = "Hi there," & .HTMLBody
.To = "abc@gmail.com"
.Subject = "Hello World"
.Send
End With
Next i
End Sub