Access 2007多个收件人电子邮件

时间:2014-01-14 11:42:25

标签: ms-access ms-access-2007 access-vba

目前,我有此代码可根据其他表单中的条件发送电子邮件。我正在构建此部门的部门已指定不止一个人可能会收到该电子邮件。如何获取Access以查看我已构建的查询。查看用户表检查谁可以接收这些电子邮件并通过电子邮件发送查询中的电子邮件列表?

Select Case Forms!FRM_CallDetails!Model.Value

Case "SM", "TW", "LM", "LV", "SV"

On Error Resume Next

DoCmd.OutputTo acOutputForm, "FRM_CallDetails", acFormatXLS, "C:\temp\WatchList.xls", False

   'Get Outlook if it's running
    Set oApp = GetObject(, "Outlook.Application")
    If Err <> 0 Then
         'Outlook wasn 't running, start it from code
         Set oApp = CreateObject("Outlook.Application")
        Started = True
    End If

                       Set oItem = oApp.CreateItem(olMailItem)
                     With oItem
                        .To = "google@google"
                        .Subject = "AutoEmail"
                        .Body = " this is the body of the email... this is a test email "
                        .Attachments.Add "C:\temp\WatchList.xls"
                        'Send the email
                        .Send
                    End With

                                    Set oItem = Nothing
                                    If Started Then
                                        oApp.Quit
                                    End If

'Display message to the user
MsgBox "A model that is on the watch list has been selected. An Automatic Email has been sent.", vbOKOnly

'Message Body Here



Case Else
     'no email
End Select

这是我正在使用的查询的SQL,我称之为Mail_List

SELECT TBL_Users.Email_Address
FROM TBL_Users
WHERE (((TBL_Users.EW_Email)="Y"));

1 个答案:

答案 0 :(得分:1)

您可以使用以下内容替换With块:

With oItem
    s = " SELECT TBL_Users.Email_Address" & _
    " FROM TBL_Users " & _
    " WHERE (((TBL_Users.EW_Email)='Y'));"
    Set rs = CurrentDb.OpenRecordset(s)
    listOfMails = ""
    While Not rs.EOF
        listOfMails = listOfMails & rs(0) & ";"
        rs.MoveNext
    Wend
    rs.Close
    Set rs = Nothing

    .To = listOfMails
    .Subject = "AutoEmail"
    .Body = " this is the body of the email... this is a test email "
    .Attachments.Add "C:\temp\WatchList.xls"
    'Send the email
    .Send
End With

为使用的三个变量添加声明:

Dim rs As Recordset
Dim s As String, listOfMails as String

这实际上并没有使用你的预制查询,而是在现场生成它,但它完成了诀窍。