I am trying to automatize an e-mail, but I am having a problem when I try to send lines from listbox; I have tried a few different ways none that were even close to working. In addition, I don't know how to use the column. I am currrently tryying to get it to work via
Dim listboxarr()
Dim i As Integer
For i = 1 To 500
' v this is a listbox
With selecteditems
listboxarr(1) = .List(i, 1)
End With
Next i
This code throws me:
Subscription out of Range
This is the code for the email:
Private Sub addcb_Click()
Dim iCtr As Long
For iCtr = 0 To Me.allitems.ListCount - 1
If Me.allitems.Selected(iCtr) = True Then
Me.selecteditems.AddItem Me.allitems.List(iCtr)
End If
Next iCtr
For iCtr = Me.allitems.ListCount - 1 To 0 Step -1
If Me.allitems.Selected(iCtr) = True Then
Me.allitems.RemoveItem iCtr
End If
Next iCtr
End Sub
Private Sub removecb_Click()
Dim iCtr As Long
For iCtr = 0 To Me.selecteditems.ListCount - 1
If Me.selecteditems.Selected(iCtr) = True Then
Me.allitems.AddItem Me.selecteditems.List(iCtr)
End If
Next iCtr
For iCtr = Me.selecteditems.ListCount - 1 To 0 Step -1
If Me.selecteditems.Selected(iCtr) = True Then
Me.selecteditems.RemoveItem iCtr
End If
Next iCtr
End Sub
Private Sub CommandButton1_Click()
Dim listboxarr()
Dim i As Integer
For i = 1 To 500
' v this is a listbox
With selecteditems
listboxarr(1) = .List(i, 1)
End With
Next i
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
.to = "Someone"
.CC = "Someone else"
.BCC = ""
.Subject = "Something"
.Body = listboxarr(1)
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
Private Sub UserForm_Initialize()
Dim itemsheet As Worksheet
Set itemsheet = Application.ActiveWorkbook.Sheets(6)
For Each itemname In itemsheet.Range("C2:C3285")
With Me.allitems
.AddItem itemname.Value
End With
Next itemname
End Sub
If you have allowed the MultiSelect property for the listbox to True, try this...
Edited Code:
And then you can use it like below...