Results 1 to 19 of 19

Thread: [RESOLVED] Sending email of selected items in listbox

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    241

    Resolved [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!

  2. #2
    Lively Member
    Join Date
    Nov 2009
    Location
    Toronto
    Posts
    103

    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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    241

    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

  4. #4
    Lively Member
    Join Date
    Nov 2009
    Location
    Toronto
    Posts
    103

    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.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    241

    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:

    Code:
    Test1Test2Test3
    when I would like it as:

    Code:
    Test1
    Test2
    Test3

  6. #6
    Lively Member
    Join Date
    Nov 2009
    Location
    Toronto
    Posts
    103

    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

  7. #7
    Lively Member
    Join Date
    Nov 2009
    Location
    Toronto
    Posts
    103

    Question 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

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    241

    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

  9. #9
    Lively Member
    Join Date
    Nov 2009
    Location
    Toronto
    Posts
    103

    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!

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    241

    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.

  11. #11
    Lively Member
    Join Date
    Nov 2009
    Location
    Toronto
    Posts
    103

    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!

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    241

    Re: Sending email of selected items in listbox

    Nope, not that I can find.

  13. #13
    Lively Member
    Join Date
    Nov 2009
    Location
    Toronto
    Posts
    103

    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!

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    241

    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...

  15. #15
    Lively Member
    Join Date
    Nov 2009
    Location
    Toronto
    Posts
    103

    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!

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    241

    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

  17. #17
    Lively Member
    Join Date
    Nov 2009
    Location
    Toronto
    Posts
    103

    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!

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    241

    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:
    &#37;0A



    Thanks again!
    Last edited by DigitalDesignz; Mar 18th, 2010 at 08:55 PM.

  19. #19
    Lively Member
    Join Date
    Nov 2009
    Location
    Toronto
    Posts
    103

    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
  •  



Click Here to Expand Forum to Full Width