By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,347 Members | 2,120 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,347 IT Pros & Developers. It's quick & easy.

Attach Multiple files to an email via listview

P: 2
I have written a small program that allows documents to be created through a visual basic form. The documents are created, an excel spreadsheet is updated, the documents are filed, and an email is sent. However, I would like to add more functionality by allowing the user to choose to email documents that previously existed. I have succeeded in having the files populate into a Listview, and they are able to select the appropriate files with checkboxes. What I would like to do is, upon clicking the button "Next", Microsoft Outlook will open, create a new email, and have the checked files already attached. Below is my current code:

Expand|Select|Wrap|Line Numbers
  1. Public Sub EMailForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2.         ListView1.Columns.Add("File Name", 230, HorizontalAlignment.Left)
  3.         ListView1.View = View.Details
  4.         ListView1.CheckBoxes = True
  5.         ListView1.Items.AddRange(Array.ConvertAll(IO.Directory.GetFiles("Filepath goes here"), Function(f) New ListViewItem(IO.Path.GetFileName(f)) With {.Tag = f}))
  6.     End Sub
  7.  
  8.     Private Sub NextBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NextBtn.Click
  9.         Dim olApp As Outlook.Application
  10.         Dim olMail As Outlook.MailItem
  11.         olApp = New Outlook.Application
  12.         olMail = olApp.CreateItem(Outlook.OlItemType.olMailItem)
  13.         olMail.Subject = "Please find the attached PAAF's"
  14.         olMail.Attachments.Add(ListView1.CheckedItems)
  15.         olMail.Display()
  16.         Dim olNs As Outlook.NameSpace
  17.         olNs = olApp.GetNamespace("MAPI")
  18.         olNs.Logon()
  19.  
  20.     End Sub
Any help would be much appreciated.
Sep 10 '13 #1
Share this Question
Share on Google+
2 Replies


P: 2
I just realized I did not actually say what my issue was. On the line:
olMail.Attachments.Add(ListView1.CheckedItems)
I get an error that says ArgumentException was unhandled.
Sep 10 '13 #2

Rabbit
Expert Mod 10K+
P: 12,366
I don't use VB much but the add attachments method is looking for a string. You are passing a collection, which is what CheckedItems returns. You need to do a loop through the checked items and add them one at a time.
Sep 10 '13 #3

Post your reply

Sign in to post your reply or Sign up for a free account.