【问题标题】:Send e-mail by check values in checkboxes通过检查复选框中的值发送电子邮件
【发布时间】:2021-02-08 05:22:17
【问题描述】:

我想检查用户窗体中的所有复选框。每个复选框的编号从 2 到 15。当一个复选框被选中时,它将从 Excel 单元格发送一封电子邮件。

例如Checkbox2 从工作表中的 A2 读取数据

我尝试制作循环。

Sub MailExcelVbaOutlookDisplay()
    Dim zm1 As Variant
    Dim ctl As Control
    Dim i As Variant
    For i = 2 To 15
        Set ctl = Me.Controls("CheckBox" & i)
        If ctl.Visible = True Then
            zm1 = i
            Dim kola As Variant
            kola = Sheets("DataBase").Range("A" & zm1.Value).Value
            Dim kolb As Variant
            kolb = Sheets("Database2").Range("B" & zm1.Value).Value
            Dim OutApp As Object
            Dim OutMail As Object
            Set OutApp = CreateObject("Outlook.Application")
            Set OutMail = OutApp.CreateItem(0)
            With OutMail
                .To = kolb
                .CC = ""
                .Subject = "subject"
                .HTMLBody = "body"
                .Attachments.Add Attachment_Box.Value
                .Display
            End With
            Set OutMail = Nothing
            Set OutApp = Nothing
         End If
    Next i
    Unload Me
End Sub

【问题讨论】:

    标签: excel vba checkbox userform


    【解决方案1】:

    我假设表单上有 14 个复选框(CheckBox2 到 CheckBox15)。我不太了解 A2 和 CheckBox2 之间的联系......但我假设 A2 持有复选框是否被选中的布尔值;在这种情况下,您不需要像直接引用 CheckBox 一样引用它。所以......你的代码的这个清理版本将生成电子邮件:

    Sub MailExcelVbaOutlookDisplay()
        
        Dim CheckBoxControl As Control
        Dim Counter As Integer
        Dim ToRecipient As String
        Dim OutApp As Object
        Dim OutMail As Object
        
        Set OutApp = CreateObject("Outlook.Application")
        
        For Counter = 2 To 15
            Set CheckBoxControl = Me.Controls("CheckBox" & Counter)
            If CheckBoxControl.Value = True Then
                ToRecipient = Sheets("Database2").Range("B" & Counter).Value
                Set OutMail = OutApp.createitem(0)
                With OutMail
                    .To = ToRecipient
                    .CC = ""
                    .Subject = "subject " & Counter
                    .HTMLBody = "body"
                    .Attachments.Add Attachment_box.Value
                    .Send
                End With
            End If
        Next
    
        Unload Me
        
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-20
      相关资源
      最近更新 更多