【发布时间】:2016-02-09 10:37:55
【问题描述】:
我可以使用 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
【问题讨论】:
-
我一直使用分号来表示多个电子邮件地址;不是逗号。
-
这仅仅是因为您没有将
toAddy拆分成一个变体数组并且从未在循环中调用数组元素吗?