[RESOLVED] Sending email of selected items in listbox
I'm trying to create a mailto hyperlink to send an email with the body being the selected items from a listbox, all on a seperate line. I have it working to where it will add the selected items but to seperate email panes.
Here is the code I have so far:
Code:
Dim i As Integer
i = ListBox1.SelectedItems.Count - 1
Do While i >= 0
System.Diagnostics.Process.Start("mailto:" & "[email protected]" & "?Subject=Subject of the email" & "&Body=" & ListBox1.SelectedItems.Item(i))
i = i - 1
Loop
ListBox1.SelectedIndex = -1
I'm sure it is probably something to do with the loop, but I'm not sure how to change it. I'm very much a beginner with .NET but have an intermediate level with basic VB6 operations. Loops and arrays are not my strong point yet so any help you can give would be greatly appreciated!
Thanks! :wave:
Re: Sending email of selected items in listbox
This will get all the selected values in the listbox:
Code:
For Each ItemInListBox As Object In ListBox1.SelectedItems
MessageBox.Show(ItemInListBox)
Next
Re: Sending email of selected items in listbox
Thanks Sean, but it still opens multiple emails with each selection per email.
Here's what I have:
Code:
For Each ItemInListBox As Object In ListBox1.SelectedItems
System.Diagnostics.Process.Start("mailto:" & "[email protected]" & "?Subject=Email subject" & "&Body=" & ItemInListBox)
Next
ListBox1.SelectedIndex = -1
Re: Sending email of selected items in listbox
Oh right sorry..
Code:
Dim ListBoxValues As String = Nothing
For Each ItemInListBox As Object In ListBox1.SelectedItems
ListBoxValues = ListBoxValues + ItemInListBox & vbLf
Next
System.Diagnostics.Process.Start("mailto:" & "[email protected]" & "?Subject=Email subject" & "&Body=" & ListBoxValues)
Re: Sending email of selected items in listbox
That works alot better Sean. Thanks!
Is there any way to get them each on their own line. They currently show as:
when I would like it as:
Re: Sending email of selected items in listbox
No problem!
Yeah I was checking that out. I thought something like this would work as it seems to give the line break in the messagebox but not in the body of the message:
Code:
Dim ListBoxValues As String = Nothing
For Each ItemInListBox As Object In ListBox1.SelectedItems
ListBoxValues = ListBoxValues + ItemInListBox & vbLf
Next
MessageBox.Show(ListBoxValues)
System.Diagnostics.Process.Start("mailto:" & "[email protected]" & "?Subject=Email subject" & "&Body=" & ListBoxValues)
Give me a few minutes :)
Re: Sending email of selected items in listbox
You might have to use this instead of your code. This works:
Code:
Dim ListBoxValues As String = Nothing
For Each ItemInListBox As Object In ListBox1.SelectedItems
ListBoxValues = ListBoxValues + ItemInListBox & vbLf
Next
' Create an Outlook application.
Dim oApp As Outlook._Application
oApp = New Outlook.Application()
' Create a new MailItem.
Dim oMsg As Outlook._MailItem
oMsg = oApp.CreateItem(Outlook.OlItemType.olMailItem)
oMsg.Subject = "Email subject"
oMsg.Body = ListBoxValues
oMsg.To = "[email protected]"
' Send
oMsg.Display() 'if you want to automatically send the message when you click ok, put o.Msg.Send()
' Clean up
oApp = Nothing
oMsg = Nothing
Also, Make sure you import this:
Code:
Imports Microsoft.Office.Interop
Re: Sending email of selected items in listbox
Where do I add the Imports Microsoft.Office.Interop? I tried at the very top above the start of the class and it highlighted Imports
Re: Sending email of selected items in listbox
Before the class even starts. You might have to go to Project > Add Reference > (and under the .NET Tab add) Microsoft.Office.Interop.Outlook
Re: Sending email of selected items in listbox
What if I don't have that? I have "Microsoft Office 12.0 Object Library" under the COM tab... among others.
Re: Sending email of selected items in listbox
Is there anything under the .NET tab that has outlook in it?
Re: Sending email of selected items in listbox
Nope, not that I can find.
Re: Sending email of selected items in listbox
hmm.. maybe add the Microsoft Office 12.0 Object Library then take a look in the .NET tab?
Re: Sending email of selected items in listbox
Nope, still not there. Not sure why it's not showing up. Tried it on my PC at home and it was the same way...
Re: Sending email of selected items in listbox
I'm pretty sure anything thats under the .NET Tab comes with the .NET framework... maybe download the .NET 3.5 Framework? If that doesn't work then try and look for the .dll file. Besides that I'm not to sure. Give that a shot and let me know how it goes.. maybe someone else has an idea to :)
Re: Sending email of selected items in listbox
I've got the .NET 3.5 SP1 framework but still not finding it. I'll look around and see if I can find the dll.
Thanks for the help btw Sean :)
Re: Sending email of selected items in listbox
Hmm.. its the strangest thing.. but yeah take a look for the dll.. if still no luck come back and post here, i'm sure someone will give you a hand! And I'll try and check it out as well. The weird part is is that I have it at home and work and I didn't have to go searching for anything.
No problem btw!
Re: Sending email of selected items in listbox
OK! Got it to work by replacing the .NET equivalent for a new line with the HTML version.
Code:
HTML new line syntax:
%0A
:)
Thanks again!
Re: [RESOLVED] Sending email of selected items in listbox
Good glad it worked out! :)