PDA

View Full Version : How to send email to recipient selected from a combo box ?



iboumiza
07-16-2011, 07:54 PM
Hi everybody,

I basically use this simple code that send automatically emails without opening Outlook window for it.

Sub SendVBAMail()

Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem

On Error Resume Next
Set OutApp = GetObject(, "Outlook.Application")
If OutApp Is Nothing Then
Set OutApp = CreateObject("Outlook.Application")
End If
On Error Goto 0

Set OutMail = OutApp.CreateItem(olMailItem)

With OutMail
.To = "name@domain.com"
.Subject = "This is the Subject line"
.Body = "Hi there"
.Send
End With

Set OutMail = Nothing
Set OutApp = Nothing

End Sub

What I'm looking for is:
How to send the email to a specific person selected from a combo box ?
I know that it's .To = "name@domain.com" that must be changed but I don't know how to make it look for a value in a combo box?!

Any help would be really appreciated!

Sincerly
iboumiza

Imdabaum
07-26-2011, 01:28 PM
Put a combo box on your form give it an appropriate name, something like cboTo.

If you are going to list multiple items in the combo box, name, email address, telephone number, other such information (idk it's up to you), you'll need to reference the column number of the email address.
e.g Me.cboTo.Column(1) --Remember columns are 0 indexed so 0 is the first column, 1= second, 2=third, etc..

If you are only displaying the email address I think Me.cboTo.Value will work.

With OutMail
.To = Me.cboTo.Column([index where email address is stored])
.Subject = "This is the Subject line"
.Body = "Hi there"
.Send
End With