Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
延迟后通过Outlook发送VBA电子邮件_Vba_Outlook_Sleep - Fatal编程技术网

延迟后通过Outlook发送VBA电子邮件

延迟后通过Outlook发送VBA电子邮件,vba,outlook,sleep,Vba,Outlook,Sleep,我对outlook和vba完全陌生。我正在尝试编写代码,在5秒钟的时间间隔后,使用 我正在使用Kernel32 Sleep函数在我的电子邮件发送代码中插入延迟,但我面临的问题是我的电子邮件发送循环处理所有睡眠计时器,然后一起发送一批电子邮件 我使用消息框确认了这一点。我想这可能是因为多线程,但我不知道如何使它成为一个原子函数。以下是我的代码片段: Public Declare PtrSafe Sub Sleep Lib "Kernel32" (ByVal dwMillisec

我对outlook和vba完全陌生。我正在尝试编写代码,在5秒钟的时间间隔后,使用

我正在使用Kernel32 Sleep函数在我的电子邮件发送代码中插入延迟,但我面临的问题是我的电子邮件发送循环处理所有睡眠计时器,然后一起发送一批电子邮件

我使用消息框确认了这一点。我想这可能是因为多线程,但我不知道如何使它成为一个原子函数。以下是我的代码片段:

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

使用、暂停宏直到指定的时间,或者如果指定的时间已到,则返回True


您可以使用计时器有限次地调用“发送电子邮件”函数。这意味着您也可以在消息之间工作,而等待或延迟功能可能会干扰标准操作

请注意:发送和接收是异步的,因此,除非使用长时间延迟,否则电子邮件可能会收到顺序错误或不代表它们之间的时间

例如:

Option Explicit

Private Declare Function SetTimer Lib "User32" (ByVal hWnd As Long, ByVal nIDEvent As Long, _
    ByVal uElapse As Long, ByVal lpTimerfunc As Long) As Long
Private Declare Function KillTimer Lib "User32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long

'TimerIDs to turn off timers. If a TimerID <> 0 then the timer is running
Private SendTimerID As Long
Private SendCount As Long

Sub SendEmails()
    Call SendStartTimer
End Sub

Private Sub SendStartTimer()
    SendCount = 0
    Call SendEventFunction
    Call ActivateTimer(5, AddressOf SendEvent, SendTimerID)
End Sub

Private Sub SendEvent(ByVal hWnd As Long, ByVal uMsg As Long, ByVal idevent As Long, ByVal Systime As Long)
    On Error Resume Next
    Call SendEventFunction
    If SendCount = 4 Then DeactivateTimer SendTimerID
End Sub

Private Sub SendEventFunction()
    Dim OutlookApp As Outlook.Application: Set OutlookApp = New Outlook.Application
    Dim OutlookMail As Outlook.MailItem: Set OutlookMail = OutlookApp.CreateItem(olMailItem)
    With OutlookMail
        .BodyFormat = olFormatHTML
        .HTMLBody = "Hi There," & .HTMLBody
        .To = "someone@somewhere.com"
        .Subject = "Hello World: " & Int(Timer) 'Indicates seconds since midnight
        .Send
    End With
    Set OutlookMail = Nothing
    SendCount = SendCount + 1
End Sub

Private Function ActivateTimer(ByVal Seconds As Long, TimerFunc As Long, ByRef TimerID As Long) 'The SetTimer call accepts milliseconds
    On Error Resume Next
    If TimerID = 0 Then TimerID = SetTimer(0, 0, Seconds * 1000, TimerFunc) 'Check to see if timer is running before call to SetTimer
End Function

Private Function DeactivateTimer(ByRef TimerID As Long)
    On Error Resume Next
    If KillTimer(0, TimerID) <> 0 Then TimerID = 0
End Function
选项显式
私有声明函数SetTimer Lib“User32”(ByVal hWnd为Long,ByVal nIDEvent为Long_
ByVal UELASS As Long,ByVal lpTimerfunc As Long)As Long
私有声明函数KillTimer Lib“User32”(ByVal hWnd为Long,ByVal nIDEvent为Long)为Long
'计时器用于关闭计时器。如果TimerID为0,则计时器正在运行
私人SendTimerID,只要
私有SendCount尽可能长
副秘书长()
呼叫SendStartTimer
端接头
私有子SendStartTimer()
SendCount=0
调用SendEventFunction
调用ActivateTimer(5,SendEvent的地址,SendTimerID)
端接头
专用子发送事件(ByVal hWnd为长,ByVal uMsg为长,ByVal idevent为长,ByVal Systime为长)
出错时继续下一步
调用SendEventFunction
如果SendCount=4,则停用发送器SendTimerID
端接头
私有子SendEventFunction()
Dim OutlookApp作为Outlook。应用程序:Set-OutlookApp=新建Outlook.Application
将OutlookMail设置为Outlook.MailItem:Set-OutlookMail=OutlookApp.CreateItem(olMailItem)
与了望邮件
.BodyFormat=olFormatHTML
.HTMLBody=“你好,”&.HTMLBody
.To=”someone@somewhere.com"
.Subject=“Hello World:”&Int(计时器)”表示从午夜开始的秒数
.发送
以
Set-OutlookMail=无
SendCount=SendCount+1
端接头
私有函数ActivateTimer(ByVal秒长,TimerFunc长,ByRef TimerID长)SetTimer调用接受毫秒
出错时继续下一步
如果TimerID=0,则TimerID=SetTimer(0,0,Seconds*1000,TimerFunc)’在调用SetTimer之前检查计时器是否正在运行
端函数
私有函数停用定时器(ByRef TimerID尽可能长)
出错时继续下一步
如果KillTimer(0,TimerID)为0,则TimerID=0
端函数

应为
Application.Wait(现在+时间值(“0:05:00”))“
看下面我的答案,我在outlook中使用Excel outlook中没有等待功能:SYea我认为outlook中没有等待功能。我知道excel肯定有,它很有魅力。非常感谢你。我不可能这么快到达这一点。如果这对你有用,请随意标记为答案:-)
Option Explicit

Private Declare Function SetTimer Lib "User32" (ByVal hWnd As Long, ByVal nIDEvent As Long, _
    ByVal uElapse As Long, ByVal lpTimerfunc As Long) As Long
Private Declare Function KillTimer Lib "User32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long

'TimerIDs to turn off timers. If a TimerID <> 0 then the timer is running
Private SendTimerID As Long
Private SendCount As Long

Sub SendEmails()
    Call SendStartTimer
End Sub

Private Sub SendStartTimer()
    SendCount = 0
    Call SendEventFunction
    Call ActivateTimer(5, AddressOf SendEvent, SendTimerID)
End Sub

Private Sub SendEvent(ByVal hWnd As Long, ByVal uMsg As Long, ByVal idevent As Long, ByVal Systime As Long)
    On Error Resume Next
    Call SendEventFunction
    If SendCount = 4 Then DeactivateTimer SendTimerID
End Sub

Private Sub SendEventFunction()
    Dim OutlookApp As Outlook.Application: Set OutlookApp = New Outlook.Application
    Dim OutlookMail As Outlook.MailItem: Set OutlookMail = OutlookApp.CreateItem(olMailItem)
    With OutlookMail
        .BodyFormat = olFormatHTML
        .HTMLBody = "Hi There," & .HTMLBody
        .To = "someone@somewhere.com"
        .Subject = "Hello World: " & Int(Timer) 'Indicates seconds since midnight
        .Send
    End With
    Set OutlookMail = Nothing
    SendCount = SendCount + 1
End Sub

Private Function ActivateTimer(ByVal Seconds As Long, TimerFunc As Long, ByRef TimerID As Long) 'The SetTimer call accepts milliseconds
    On Error Resume Next
    If TimerID = 0 Then TimerID = SetTimer(0, 0, Seconds * 1000, TimerFunc) 'Check to see if timer is running before call to SetTimer
End Function

Private Function DeactivateTimer(ByRef TimerID As Long)
    On Error Resume Next
    If KillTimer(0, TimerID) <> 0 Then TimerID = 0
End Function