如何从Outlook收件箱中自动发送延迟电子邮件以使用VBA?

时间:2016-12-23 07:33:45

标签: vba email outlook delay outlook-vba

我是VBA的新手,我想创建一个规则(脚本),可以自动转发我的Outlook收件箱中收到的指定延迟的电子邮件? 你能否告诉我如何实现这个目标?

我试过了:

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

也尝试过:

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

无效。

第二个代码实际上冻结了我的整个Outlook应用程序,这不是我想要的效果。我只想延迟重新发送电子邮件......

1 个答案:

答案 0 :(得分:1)

尝试使用DeferredDeliveryTime Property设置邮件发送时间。

实施例

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