I'm new in the ways of VBA and I want to create a rule (script) that can automatically forward an email received in my Outlook inbox with a specified delay? Can you please give me some instructions on how can I achieve this?
I tried:
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMiliseconds As Long)
Sub send(Item As Outlook.MailItem)
Set fwd = Item.Forward
fwd.Recipients.Add "mail@email.com"
Sleep (10000)
fwd.send
End Sub
Also tried:
Sub WasteTime(Finish As Long)
Dim NowTick As Long
Dim EndTick As Long
EndTick = GetTickCount + (Finish * 1000)
Do
NowTick = GetTickCount
DoEvents
Loop Until NowTick >= EndTick
End Sub
Sub send(Ite As Outlook.mailItem)
Set fwd = Item.Forward
fwd.Recipients.Add "mail@email.com"
WasteTime (10)
fwd.send
End Sub
to no effect.
The 2nd code actually freezes my entire Outlook app which is not my desired effect. I only want to delay the re-sending of the email...
Try using DeferredDeliveryTime Property Which sets the time mail is to be delivered.
Example
Option Explicit
Public Sub Example()
Dim Item As Outlook.MailItem
Set Item = Application.CreateItem(0)
With Item
.Subject = "test"
.To = "0m3r"
.DeferredDeliveryTime = DateAdd("n", 10, Now)
Debug.Print Item.DeferredDeliveryTime
.Send
End With
Set Item = Nothing
End Sub
DateAdd("n", 10, Now)
+--------+-----------------+
| Value | Explanation |
+--------+-----------------+
| yyyy | Year |
| q | Quarter |
| m | Month |
| y | Day of the year |
| d | Day |
| w | Weekday |
| ww | Week |
| h | Hour |
| n | Minute |
| s | Second |
+--------+-----------------+
来源:https://stackoverflow.com/questions/41297105/how-can-i-automatically-send-an-email-with-delay-from-an-outlook-inbox-usign-vba