发送电子邮件Outlook 2013作为后台进程

时间:2018-10-10 22:05:35

标签: vba outlook outlook-vba

我写了一个小宏,每个星期五向办公室不同部门的大约20个人发送电子邮件。 我这样做的方法是先创建一个带有名称和电子邮件的电子表格,然后遍历整个电子表格以更改适当的信息。我希望电子邮件发给每个人,所以不,其他收件人不应该抄送或密送。

有效。但是,为了避免出现问题,我暂停了一下,以便每6秒钟每6秒钟发送一次电子邮件,每间隔10秒钟。我相信暂停会造成延迟,并且在完成工作后,我将无法使用Outlook(检查其他电子邮件或任务)。

是否可以让Outlook以某种“静默模式”发送每封电子邮件?

此处,代码:

Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)

Sub PrepareEmail()
.
.'A bunch of variable and simple data manipulation is done here.
.
For emailLooper = 0 To BrokerForm.LstBoxBrokers.ListCount - 1
Set oMail = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")

iConf.Load -1    ' CDO Source Defaults
Set Flds = iConf.Fields
With Flds
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mySMTPserver"
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "username"
    .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
    .Update
End With

With oMail

Set .Configuration = iConf
.Subject = asunto
.HTMLBody = emailText & signatureEmail

    destName = BrokerForm.LstBoxBrokers.List(emailLooper, 0)
    emailAddress = BrokerForm.LstBoxBrokers.List(emailLooper, 1)

    .HTMLBody = Replace(.HTMLBody, "#name#", destName)
    .To = emailAddress
    .From = senderEmail
    .Send

    emailsSent = emailsSent + 1

    Sleep (6000)

    If emailLooper / 10 = Fix(emailLooper / 10) Then

        Sleep (11000)

    End If

End With


Set oMail = Nothing
Set iConf = Nothing
Set Flds = Nothing

Next emailLooper

2 个答案:

答案 0 :(得分:0)

您可以尝试使用Sleep()函数来创建循环计时器,而不是使用DoEvents API函数。

尝试看看是否有帮助:

Dim startTimer as single, sleepTime as single
sleepTime = 6

With oMail

Set .Configuration = iConf
.Subject = asunto
.HTMLBody = emailText & signatureEmail

    destName = BrokerForm.LstBoxBrokers.List(emailLooper, 0)
    emailAddress = BrokerForm.LstBoxBrokers.List(emailLooper, 1)

    .HTMLBody = Replace(.HTMLBody, "#name#", destName)
    .To = emailAddress
    .From = senderEmail
    .Send

    emailsSent = emailsSent + 1

    startTimer = Timer
    Do While Timer < startTimer + sleepTime
        DoEvents
    Loop

    If emailLooper / 10 = Fix(emailLooper / 10) Then

        startTimer = Timer
        Do While Timer < startTimer + sleepTime
            DoEvents
        Loop

    End If

End With

答案 1 :(得分:0)

如K.Dᴀᴠɪs所述,您可以使用Loop而不是Sleep()。

如果使用Sleep(),Outlook将锁定您直到睡眠期结束。您可以使用CTRL+BREAK来中断宏,但是Outlook不会接受来自键盘或鼠标的输入。

有关更多信息,您可以参考以下链接:

Pausing or Delaying VBA Using Wait, Sleep or A Loop