vba - 无法发送多个 Outlook 邮件

标签 vba excel outlook

我可以使用 Excel VBA 发送单个 Outlook 消息。但是,我想遍历我的行并为​​满足特定条件的每一行发送一封电子邮件。

不幸的是,当我将电子邮件代码放在 for 循环中时,只有一封电子邮件被发送或根本没有发送(取决于我如何构建代码)。

是否有关于多次调用 Outlook 的信息我应该知道?

Private Sub CommandButton1_Click()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim myValue As Variant
    Dim contactRange As Range
    Dim cell As Range
    Dim toAddy As String, nextAddy As String
    Dim i As Integer 
    Set contactRange = Me.Range("ContactYesNo")

    myValue = InputBox("Enter body of email message.")

    For Each cell In contactRange

        If Range(Cells(cell.Row, cell.Column).Address).Value = "Yes" Then
            nextAddy = Range(Cells(cell.Row, cell.Column).Address).Offset(0, 5).Value

            toAddy = nextAddy & ", " & toAddy

        End If

    Next cell

    If Len(toAddy) > 0 Then

        toAddy = Left(toAddy, Len(toAddy) - 2)

    End If

For i = 0 To 1 'short loop for testing purposes

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    With OutMail

        .To = toAddy 
        .CC = ""
        .BCC = ""
        .Subject = "test email"
        .Body = myValue
        .Send
    End With

    Set OutMail = Nothing
    Set OutApp = Nothing

Next i


End Sub 

最佳答案

从循环中取出 CreateObject 行:

Set OutApp = CreateObject("Outlook.Application")
For i = 0 To 1 'short loop for testing purposes
    Set OutMail = OutApp.CreateItem(0)
    ...

关于vba - 无法发送多个 Outlook 邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35261782/

相关文章:

ms-access - 我如何知道单击了多选 Microsoft Access 列表框中的哪个项目?

vba - Excel UDF 筛选范围

c# - Outlook 2013 执行插入签名

excel - Review 的跟踪更改使我的 VBA 项目无法查看

vba - 仅当用户修改现有值时禁用消息框弹出

java - 根据textarea的输出更新excel文件

c# - MailItem.HtmlBody 引发未实现异常

C# 双 Outlook 2007/2010 VSTO 加载项

vba - 删除行时触发 Excel Worksheet_Change 事件

c++ - 如何检测 MS Excel 的语言