2014-01-14 41 views
0

目前我有這個代碼發送電子郵件根據另一種形式的標準。我正在爲此創建的部門指定了多個人可能會收到該電子郵件。我如何獲得Access查看我已經構建的查詢。查看用戶表時會檢查哪些人可以收到這些電子郵件並通過電子郵件從查詢中發送電子郵件列表?訪問2007多個收件人電子郵件

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 = "[email protected]" 
         .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 

下面是我使用的查詢,我已呼籲Mail_List

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

回答

1

的SQL,你可以用下面的更換你帶塊:

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 

這並不實際使用您的預製查詢,而是在現場生成它,但它可以完成。

+0

完美的是,這正是我所需要的,它實時生成查詢的事實對於即時更新更好。完美! – ASM2701

相關問題