2017-05-03 29 views
0

我正在使用Access,窗體和子窗體,並且我試圖通過複選框循環,如果它們被選中,則發送一封電子郵件給已關閉的人。如果複選框被選中,試圖發送電子郵件給人們

問題是我找不到方法或數據成員。 該行引發錯誤。 Me.qry_Ryan_Emails.Work_Email

Option Compare Database 

Private Sub Command1_Click() 

For Each ctrl In Me.qry_Ryan_Emails.Controls 

    If TypeName(ctrl) = "CheckBox" Then 
     If ctrl.Enabled = True Then 
      'Debug.Print TypeName(ctrl) 

      Set OutApp = CreateObject("Outlook.Application") 
       Set OutMail = OutApp.CreateItem(0) 
       eSubject = Me.Subject.Text 
       eBody = Me.Message.Text 

       On Error Resume Next 

       With OutMail 
        .To = Me.qry_Ryan_Emails.Work_Email 
        .CC = "" 
        .BCC = "" 
        .Subject = eSubject 
        .BodyFormat = olFormatHTML 
        .Display 
        .HTMLBody = eBody & vbCrLf & .HTMLBody 
        '.Send 
       End With 

       On Error GoTo 0 
       Set OutMail = Nothing 
       Set OutApp = Nothing 


     End If 

    End If 
Next ctrl 

End Sub 

最後,是它更好地串連所有電子郵件收件人到一個電子郵件,只發送一個電子郵件,而不是多個電子郵件?這可能是一個更好的方式。任何想法,任何人?

回答

0

這適用於我。

Option Compare Database 

Private Sub Command1_Click() 

AllEmails = "" 
For Each ctrl In Me.qry_Ryan_Emails.Controls 

    If TypeName(ctrl) = "CheckBox" Then 
     If ctrl.Enabled = True Then 
      AllEmails = AllEmails & " " & Me!qry_Ryan_Emails.Form.Work_Email 
     End If 
    End If 

Next ctrl 

Set OutApp = CreateObject("Outlook.Application") 
    Set OutMail = OutApp.CreateItem(0) 
    eSubject = Me!Subject 
    eBody = Me!Message 

    On Error Resume Next 

    With OutMail 
     .To = AllEmails 
     .CC = "" 
     .BCC = "" 
     .Subject = eSubject 
     .BodyFormat = olFormatHTML 
     .Display 
     .HTMLBody = eBody & vbCrLf & .HTMLBody 
     .Send 
    End With 

    On Error GoTo 0 
    Set OutMail = Nothing 
    Set OutApp = Nothing 

End Sub 

此行將所有電子郵件連接成一個字符串。

AllEmails = AllEmails & " " & Me!qry_Ryan_Emails.Form.Work_Email 

然後,我只發送1封電子郵件給該組。

此鏈接對於引用窗體和子窗體上的控件非常有用。

http://access.mvps.org/access/forms/frm0031.htm

0

你基本上可以用列表框,選擇ListBox中的多個項目同樣的事情。

Option Compare Database 

Private Sub Command1_Click() 
Dim varItem As Variant 
Dim lngRow As Long 
Dim strMsg As String 

AllEmails = "" 

With Me.List_Emails 
    For lngRow = 0 To .ListCount - 1 
     If .Selected(lngRow) Then 
      AllEmails = AllEmails & .Column(2, lngRow) 
     End If 
    Next lngRow 
End With 


Set OutApp = CreateObject("Outlook.Application") 
    Set OutMail = OutApp.CreateItem(0) 
    eSubject = Me!Subject 
    eBody = Me!Message 

    On Error Resume Next 

    With OutMail 
     .To = AllEmails 
     .CC = "" 
     .BCC = "" 
     .Subject = eSubject 
     .BodyFormat = olFormatHTML 
     .Display 
     .HTMLBody = eBody & vbCrLf & .HTMLBody 
     '.Send 
    End With 

    On Error GoTo 0 
    Set OutMail = Nothing 
    Set OutApp = Nothing 

End Sub 
相關問題