选中复选框并将项目发送到电子邮件

Selection of checkbox and send items to email

提问人:Learner11 提问时间:11/16/2023 最后编辑:Learner11 更新时间:11/16/2023 访问量:52

问:

我在使用 VBA 发送电子邮件时遇到问题。所以我在工作表中有复选框和项目,我想在选中复选框后将项目发送到电子邮件。如果未选中,则不执行任何操作。 我有这个错误,说“下标超出范围”。我不知道如何解决。这是我测试的电子表格的图像,这是我的代码:

Private Sub sendEmail(arrType, arrItem, arrQuantity, arrUnit)

    Dim i      As Integer
    Dim objOutlook As Object
    Set objOutlook = CreateObject("outlook.application"
    Dim ws     As Worksheet
    Dim strSubject As String
    Dim strBody As String
    Dim strType As String
    Dim strItem As String
    Dim strQuantity As String
    Dim strUnit As String
    Dim strTable As String
    Dim strHTML As String
    Set ws = ThisWorkbook.Worksheets("Data")
    
    strSubject = "Testing"
    strBody = "<html>"
    strBody = strBody & "Please see the order details below for your reference:<br><br>"
    strTable = "<br><table border = 2><tbody>"
    strTable = strTable & "<tr>"
    strTable = strTable & "<th align = center> Type</th>"
    strTable = strTable & "<th align = center> Item</th>"
    strTable = strTable & "<th align = center> Quantity</th>"
    strTable = strTable & "<th align = center> unit</th>"
    strTable = strTable & "<tr/>"
    
    For i = 4 To UBound(arrType)
        strType = arrType(i)
        strItem = arrItem(i)
        strQuantity = arrQuantity(i)
        strUnit = arrUnit(i)
        
        strTable = strTable & "<tr><td>" & strType & "</td>"
        strTable = strTable & "<td>" & strItem & "</td>"
        strTable = strTable & "<td>" & strQuantity & "</td>"
        strTable = strTable & "<td>" & strUnit & "</td></tr>"
    Next
    strTable = strTable & "</tbody></table><br>"
    strHTML = strBody & strTable & "</html>"
    
    
    
    If MsgBox("Are you sure you want to submit? ", vbYesNo, "Submit Confirmation") = vbYes Then
        Dim objEmail As Object
        Set objEmail = objOutlook.CreateItem(0)
        With objEmail
            .To = ""
            .Subject = "testing"
            .HTMLBody = strHTML
            .Display
            .Send
        End With
        MsgBox "Thanks for the order. Your order details are sent successfully.", vbxOKOnly, "Operation Successful"
    Else
        Exit Sub
    End If
End Sub
Private Sub itemStored(arrType, arrItem, arrQuantity, arrUnit)


    Set ws = ThisWorkbook.Worksheets("Data")
    
    Dim i      As Long
    
    Dim cb     As CheckBox
    
    
    For Each cb In CheckBoxes
        
        If cb.Value = 1 Then
            
            arrType(i) = ws.Cells(i + 4, "I").Value
            
            arrItem(i) = ws.Cells(i + 4, "I").Value
            
            arrQuantity(i) = ws.Cells(i + 4, "I").Value
            
            arrUnit(i) = ws.Cells(i + 4, "I").Value
            
            i = i + 1
            
        End If
        
    Next
    
End Sub
Private Sub cmdbtnShow_Click()
    OrderForm.Show
End Sub
Private Sub CommandButton2_Click()
    Dim arrType() As Variant
    Dim arrItem() As Variant
    Dim arrQuantity As Integer
    Dim arrUnit As String

    Call itemStored(arrType, arrItem, arrQuantity, arrUnit)
    Call sendEmail(arrType, arrItem, arrQuantity, arrUnit)
End Sub

选中复选框后,左侧的项目将发送到电子邮件。如果没有,则会发生记录。我尝试让arrType和arrItem对应于sendEmail,但仍然不起作用。

Excel VBA 复选框 Outlook

评论

0赞 Tim Williams 11/16/2023
运行代码的工作簿中没有工作表“数据”?如果不是这样,那么您需要告诉我们哪一行抛出了该错误。此外,您没有调整任何正在传递的数组的大小 - 这也可能导致相同的错误。并且您从同一列中获取所有数组值。
0赞 Tim Williams 11/16/2023
那么问题就是你没有调整数组的大小。在尝试向它们添加任何项目之前,您需要这样做。
0赞 Tim Williams 11/16/2023
For Each cb In CheckBoxes- 这些复选框在哪里?不清楚您发布的代码在哪里 - 在工作表代码模块中?常规模块?你展示的表格上有什么?
0赞 Learner11 11/16/2023
For i = 4 To UBound(arrType)这行代码出现错误。此外,工作表“数据”是我的 excel 工作簿。如何调整数组的大小?
0赞 Tim Williams 11/16/2023
通常类似于(例如)Dim arrType(0 to 10)

答:

1赞 Tim Williams 11/16/2023 #1

试试这个 - 我认为使用数组集合来保存所选行中的信息更容易。

Option Explicit

Private Sub sendEmail(colItems As Collection)

    Dim objOutlook As Object, arr, ws As Worksheet, objEmail As Object
    Dim i As Integer, strSubject As String, strBody As String, strTable As String, strHTML As String
    
    strSubject = "Testing"
    strBody = "<html>"
    strBody = strBody & "Please see the order details below for your reference:<br><br>"
    strTable = "<table border=2><tbody>"
    strTable = strTable & "<tr><th align='center'>Type</th>"
    strTable = strTable & "<th align='center'>Item</th>"
    strTable = strTable & "<th align='center'>Quantity</th>"
    strTable = strTable & "<th align='center'>unit</th></tr>"
    For Each arr In colItems 'loop arrays in collection
        strTable = strTable & "<tr><td>" & Join(arr, "</td><td>") & "</td></tr>"
    Next arr
    strTable = strTable & "</tbody></table>"
    strHTML = strBody & strTable & "<br></html>"
    
    If MsgBox("Are you sure you want to submit? ", vbYesNo, "Submit Confirmation") = vbYes Then
        
        Set objOutlook = CreateObject("outlook.application")
        Set objEmail = objOutlook.CreateItem(0)
        With objEmail
            .To = ""
            .Subject = strSubject
            .HTMLBody = strHTML
            .Display
            '.send
        End With
        MsgBox "Thanks for the order. Your order details are sent successfully.", _
               vbOKOnly, "Operation Successful"
    Else
        Exit Sub
    End If
End Sub

'collect information from each row associated with a checked ActiveX checkbox...
Private Function selectedItems() As Collection
    Dim obj As Object, ws As Worksheet
    Set selectedItems = New Collection
    Set ws = ThisWorkbook.Worksheets("Data")
    For Each obj In ws.OLEObjects   'loop all controls on the sheet
        If TypeName(obj.Object) = "CheckBox" Then 'is a checkbox?
            If obj.Object.Value = True Then       'checkbox is checked?
                With obj.TopLeftCell.EntireRow    'the row the checkbox is on
                    'add array of row values to the collection
                    selectedItems.Add Array(.Columns("D").Value, .Columns("E").Value, _
                                            .Columns("F").Value, .Columns("G").Value)
                End With
            End If 'checked
        End If
    Next obj
End Function

Private Sub CommandButton2_Click()
    Dim selItems As Collection
    Set selItems = selectedItems() 'returns a Collection of arrays
    If selItems.Count > 0 Then
        sendEmail selItems
    Else
        MsgBox "No items selected"
    End If
End Sub

评论

0赞 Learner11 11/16/2023
谢谢,它有效。如何在数量列的末尾包含总计?
0赞 Tim Williams 11/16/2023
在循环访问集合以创建表时创建总计。试一试