How to assign multiple listbox values to one variable and put those in Email “TO” field
-
21-12-2019 - |
Question
I am trying to be able to select multiple values in two listboxes and assign to two separate variables. I want to then take those variables containing the selections and generate an email with the variable contents populating the "TO" field in an outlook email. Right now I am getting a Run-Time Error 94 - Invalid Use of Null.
Thanks for all your help!
Dim EAddress, MAddress As String
Public Sub UserForm_Initialize()
Emailfrm.EmpEmaillb.RowSource = "Searched_Employee_Email"
Emailfrm.ManagerEmaillb.RowSource = "Searched_Manager_Email"
End Sub
Public Sub Email_Click()
Dim OLobjMsg, NewMsg As Object
EAddress = Emailfrm.EmpEmaillb.Value
MAddress = Emailfrm.ManagerEmaillb.Value
Set objMsg = CreateObject("Outlook.Application")
objMsg.Session.Logon
Set NewMsg = objMsg.CreateItem(0)
With NewMsg
.To = EAddress & MAddress
.Subject = "BT Employee Database Inquiry Email"
'.Body = "Have a great weekend!"
End With
Unload Me
NewMsg.Display
End Sub
Solution
Asuming that your listboxes contain valid email addresses, I propose to
- define
ListBox1.MultiSelect = fmMultiSelectMulti
- select multiple mail addresses by using Ctrl-Click
- concatenate all selected addresses from the Listbox into one single string, e.g.
_
Private Sub CommandButton1_Click()
Dim LBCnt As Integer, AllAddr As String
AllAddr = ""
For LBCnt = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(LBCnt) Then
If AllAddr = "" Then
AllAddr = ListBox1.List(LBCnt)
Else
AllAddr = AllAddr & ";" & ListBox1.List(LBCnt)
End If
End If
Next LBCnt
Debug.Print AllAddr
End Sub
- repeat for the 2nd listbox ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow