|
-
Mar 17th, 2010, 09:01 AM
#1
Thread Starter
Addicted Member
[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!
-
Mar 17th, 2010, 11:19 AM
#2
Lively Member
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
-
Mar 17th, 2010, 12:13 PM
#3
Thread Starter
Addicted Member
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
-
Mar 17th, 2010, 12:27 PM
#4
Lively Member
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)
Last edited by Sean04; Mar 17th, 2010 at 12:30 PM.
-
Mar 17th, 2010, 12:57 PM
#5
Thread Starter
Addicted Member
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:
-
Mar 17th, 2010, 01:13 PM
#6
Lively Member
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
-
Mar 17th, 2010, 01:59 PM
#7
Lively Member
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
-
Mar 17th, 2010, 02:07 PM
#8
Thread Starter
Addicted Member
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
-
Mar 17th, 2010, 02:11 PM
#9
Lively Member
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
< Rate a post if it helps! 
-
Mar 17th, 2010, 02:58 PM
#10
Thread Starter
Addicted Member
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.
-
Mar 17th, 2010, 03:03 PM
#11
Lively Member
Re: Sending email of selected items in listbox
Is there anything under the .NET tab that has outlook in it?
< Rate a post if it helps! 
-
Mar 17th, 2010, 06:27 PM
#12
Thread Starter
Addicted Member
Re: Sending email of selected items in listbox
Nope, not that I can find.
-
Mar 18th, 2010, 07:50 AM
#13
Lively Member
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?
< Rate a post if it helps! 
-
Mar 18th, 2010, 12:11 PM
#14
Thread Starter
Addicted Member
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...
-
Mar 18th, 2010, 12:28 PM
#15
Lively Member
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
< Rate a post if it helps! 
-
Mar 18th, 2010, 07:02 PM
#16
Thread Starter
Addicted Member
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
-
Mar 18th, 2010, 07:31 PM
#17
Lively Member
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!
< Rate a post if it helps! 
-
Mar 18th, 2010, 08:51 PM
#18
Thread Starter
Addicted Member
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!
Last edited by DigitalDesignz; Mar 18th, 2010 at 08:55 PM.
-
Mar 20th, 2010, 09:17 PM
#19
Lively Member
Re: [RESOLVED] Sending email of selected items in listbox
Good glad it worked out!
< Rate a post if it helps! 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|