0

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

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Mircea
  • 29
  • 1
  • 4

1 Answers1

2

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 Function

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          |
+--------+-----------------+
0m3r
  • 12,286
  • 15
  • 35
  • 71
  • 2
    The problem with `DeferredDeliveryTime` is you can't set it to a specific "second" (say, in 10 seconds from now), it just won't work as expected. Even though you can set seconds to it, it won't take it, it rounds it to the nearest minute. I haven't figured it out completely yet, but it certainly isn't as predictable as expected. – Sam White Jun 12 '19 at 13:59