Vba 循环,从DAO记录集发送Outlook邮件,而不是在整个表中循环

Vba 循环,从DAO记录集发送Outlook邮件,而不是在整个表中循环,vba,ms-access,Vba,Ms Access,我正在尝试使用特定的电子邮件帐户(不是默认帐户)从Outlook 2010发送电子邮件 电子邮件基于一个静态模板,该模板从表(senders_表)中提取收件人、主题和电子邮件正文中几个变量字段的数据 代码没有在我的表中的所有记录中循环。电子邮件通过指定的帐户发出,并从表中提取适当的数据,但在第一次记录后停止 Private Sub test_Click() 'You must add a reference to the Microsoft Outlook Library Dim OutApp

我正在尝试使用特定的电子邮件帐户(不是默认帐户)从Outlook 2010发送电子邮件

电子邮件基于一个静态模板,该模板从表(senders_表)中提取收件人、主题和电子邮件正文中几个变量字段的数据

代码没有在我的表中的所有记录中循环。电子邮件通过指定的帐户发出,并从表中提取适当的数据,但在第一次记录后停止

Private Sub test_Click()

'You must add a reference to the Microsoft Outlook Library
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim strbody As String
Dim stremail As String
Dim strsubject As String

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

Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("Senders_Table")
With rs

    If .EOF And .BOF Then
        MsgBox "No emails will be sent becuase there are no records assigned from the list", vbInformation
    Else
        Do Until .EOF

            stremail = ![email]
            strsubject = ![address]
            strbody = "Dear " & ![name] & "," & _
              Chr(10) & Chr(10) & "Some kind of greeting" & ![address] & "!" & _
              "  email message body goes here"

            .Edit
            .Update
            .MoveNext

        Loop

    End If
End With

On Error Resume Next
With OutMail
    .To = stremail
    .CC = ""
    .BCC = ""
    .Subject = strsubject
    .Body = strbody

    .SendUsingAccount = OutApp.Session.Accounts.Item(2)
    .Send
End With
On Error GoTo 0

If Not rs Is Nothing Then
    rs.Close
    Set rs = Nothing
End If

Set OutMail = Nothing
Set OutApp = Nothing

End Sub

您需要在循环中移动发送电子邮件代码,以便为每个记录发送电子邮件。大概是这样的:

Set OutApp = CreateObject("Outlook.Application")

Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("Senders_Table")
With rs
    If .EOF And .BOF Then
        MsgBox "No emails will be sent becuase there are no records assigned from the list", vbInformation
    Else
        Do Until .EOF
            stremail = ![email]
            strsubject = ![address]
            strbody = "Dear " & ![name] & "," & _
                      Chr(10) & Chr(10) & "Some kind of greeting" & ![address] & "!" & _
                      "  email message body goes here"

            '.Edit
            '.Update

            Set OutMail = OutApp.CreateItem(olMailItem)
            With OutMail
                .To = stremail
                .CC = ""
                .BCC = ""
                .Subject = strsubject
                .Body = strbody

                .SendUsingAccount = OutApp.Session.Accounts.Item(2)
                .Send
            End With            
            .MoveNext
        Loop

    End If
End With

您需要在循环中移动发送电子邮件代码,以便为每个记录发送电子邮件。大概是这样的:

Set OutApp = CreateObject("Outlook.Application")

Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("Senders_Table")
With rs
    If .EOF And .BOF Then
        MsgBox "No emails will be sent becuase there are no records assigned from the list", vbInformation
    Else
        Do Until .EOF
            stremail = ![email]
            strsubject = ![address]
            strbody = "Dear " & ![name] & "," & _
                      Chr(10) & Chr(10) & "Some kind of greeting" & ![address] & "!" & _
                      "  email message body goes here"

            '.Edit
            '.Update

            Set OutMail = OutApp.CreateItem(olMailItem)
            With OutMail
                .To = stremail
                .CC = ""
                .BCC = ""
                .Subject = strsubject
                .Body = strbody

                .SendUsingAccount = OutApp.Session.Accounts.Item(2)
                .Send
            End With            
            .MoveNext
        Loop

    End If
End With

这对我有用。我已经查询了2个字段[电子邮件];[地址];;[姓名]

我知道这是一个旧的线程,但我还没有找到任何代码,不会使安全消息弹出。希望这对某人有所帮助

子SendEmailFromQuery()
'必须添加对Microsoft Outlook库的引用
将OutApp设置为Outlook.Application
将OutMail设置为Outlook.MailItem
像弦一样暗的链子
把邮件当作字符串
作为字符串的Dim strsubject
Set-OutApp=CreateObject(“Outlook.Application”)
Dim rs作为DAO.Recordset
Set rs=CurrentDb.OpenRecordset(“Query2”)''在此处添加查询
用rs
如果.EOF和.BOF,则
MsgBox“由于列表中没有指定记录,因此不会发送电子邮件”,vbInformation
其他的
直到。EOF
stremail=![电子邮件]''查询2字段[电子邮件];[地址];;[姓名]
strsubject=![地址]
strbody=“亲爱的”&![姓名]&“,”和_
圣诞(10)&圣诞(10)&“某种问候”&![地址]&“!”&_
“电子邮件正文位于此处”
出错时继续下一步
Set-OutMail=OutApp.CreateItem(olMailItem)
发邮件
.To=stremail
.CC=“”
.BCC=“”
.Subject=strsubject
.车身=车身
.SendUsingAccount=OutApp.Session.Accounts.Item(2)
.发送
以
.下一步
环
'在出现错误时转到0
如果不是的话,那么rs什么都不是
rs.Close
设置rs=无
如果结束
发送邮件=无
设置应用程序=无
如果结束
以

End Sub
这对我很有用。我已经查询了2个字段[电子邮件];[地址];;[姓名]

我知道这是一个旧的线程,但我还没有找到任何代码,不会使安全消息弹出。希望这对某人有所帮助

子SendEmailFromQuery()
'必须添加对Microsoft Outlook库的引用
将OutApp设置为Outlook.Application
将OutMail设置为Outlook.MailItem
像弦一样暗的链子
把邮件当作字符串
作为字符串的Dim strsubject
Set-OutApp=CreateObject(“Outlook.Application”)
Dim rs作为DAO.Recordset
Set rs=CurrentDb.OpenRecordset(“Query2”)''在此处添加查询
用rs
如果.EOF和.BOF,则
MsgBox“由于列表中没有指定记录,因此不会发送电子邮件”,vbInformation
其他的
直到。EOF
stremail=![电子邮件]''查询2字段[电子邮件];[地址];;[姓名]
strsubject=![地址]
strbody=“亲爱的”&![姓名]&“,”和_
圣诞(10)&圣诞(10)&“某种问候”&![地址]&“!”&_
“电子邮件正文位于此处”
出错时继续下一步
Set-OutMail=OutApp.CreateItem(olMailItem)
发邮件
.To=stremail
.CC=“”
.BCC=“”
.Subject=strsubject
.车身=车身
.SendUsingAccount=OutApp.Session.Accounts.Item(2)
.发送
以
.下一步
环
'在出现错误时转到0
如果不是的话,那么rs什么都不是
rs.Close
设置rs=无
如果结束
发送邮件=无
设置应用程序=无
如果结束
以
末端接头