How can I automatically send an email with delay from an Outlook inbox usign VBA?

廉价感情. 提交于 2019-12-02 19:17:01

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/41297105/how-can-i-automatically-send-an-email-with-delay-from-an-outlook-inbox-usign-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!