Hi,
On my userform I have a ListBox that is auto generated based on a list on my main worksheet. From the list on the userform the user can select which people they want the file to be sent to. The code I wrote loops through each of the users in the ListBox to see which ones are selected. If they are selected than an email is sent. The code always works until it gets to the last name on the ListBox. Once it gets to the last name I get a "run-time error - Could not get the Selected property. Invalid argument.
On my userform I have a ListBox that is auto generated based on a list on my main worksheet. From the list on the userform the user can select which people they want the file to be sent to. The code I wrote loops through each of the users in the ListBox to see which ones are selected. If they are selected than an email is sent. The code always works until it gets to the last name on the ListBox. Once it gets to the last name I get a "run-time error - Could not get the Selected property. Invalid argument.
Code:
Private Sub CommandButton1_Click()
Dim LastRow As Long
Dim toList As String
Dim eSubject As String
Dim eBody As String
Dim curColumn As Long
Dim ctrl As Control
Dim i As Integer
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
curColumn = 1
LastRow = Worksheets("sht_data").Cells(Rows.Count, curColumn).End(xlUp).Row
For i = 7 To LastRow
If ListBox1.Selected(i - 6) = True Then
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
toList = Cells(i, 6)
eSubject = "Hotshop Metrics"
eBody = "Please see your attached Hotshop metrics report"
On Error Resume Next
With OutMail
.To = toList
.CC = ""
.BCC = ""
.Subject = eSubject
.BodyFormat = olFormatHTML
.Display
.HTMLBody = eBody & vbCrLf & .HTMLBody
'.Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
Else
End If
Next i
End Sub