• Subject: Re: Add attachments to message via VB.NET 2005
  • Author: Sue Mosher [MVP-Outlook]
  • Date: 12 Nov 2007
  • References:
This statement is invalid:

oMsg.Attachments.Add(oAttachment)

You've declared oAttachment as an Attachment object, but when you're working with files, the Attachments.Add method takes a string as its first parameter -- the full path and name of the file you want to attach.

Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54

"Jac Tremblay" <jac.tremblay@donotspam.com> wrote in message news:C5BE9D83-F5E3-433F-8A94-626537E40905@microsoft.com...
> Hi.
> I am trying to complete an application that displays a form to create and
> send a message via Outlook. This form has a Browse for folder button that
> allows the user to select attachments to send along with the message. If the
> user clicks on it, an arraylist is created with the selected file names.
> Then, when he (or she) presses the Send button, the selected files in the
> arraylist sould be attached to the message. But I get an error:
> System.InvalidCastException was unhandled
> Message="Unable to cast object of type 'System.String' to type
> 'Microsoft.Office.Interop.Outlook.Attachment'."
> I understand the error but do not know how to correct it. Here is the code.
>
> ' ***** Beginning of code *****
> Imports Microsoft.Office.Interop
>
> Public Class frmOutlook
>
> Inherits System.Windows.Forms.Form
>
> Dim arlAttachments As ArrayList
>
> Private Sub cmdBrowse_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) _
> Handles cmdBrowse.Click
>
> With odlgAttachment
> .InitialDirectory = "C:\"
> .Filter = "All Files (*.*)|*.*|HTML Files
> (*.htm;*.html)|*.htm|Microsoft Mail " & _
> "Documents (*.msg)|*.msg|Word Documents (*.doc)|*.doc|Excel
> Files(*.xl*)|*.xl*|Excel " & _
> "Worksheets (*.xls)|*.xls|Excel Charts
> (*.xlc)|*.xlc|PowerPoint Presentations " & _
> "(*.ppt)|*.ppt|Text Files (*.txt)|*.txt"
> .FilterIndex = 1
>
> If .ShowDialog() = Windows.Forms.DialogResult.OK Then
> If IsNothing(arlAttachments) Then
> arlAttachments = New ArrayList()
>
> lstAttachments.Items.Clear()
> End If
> 'Dim oApp As New Outlook.Application()
> 'Dim oAttachment As Outlook.Attachment
> 'oAttachment = CType(.FileName, Outlook.Attachment)
> 'arlAttachments.Add(oAttachment) ' That doesn't work either...
>
> ' *** The error is either on the following line or in the next Sub...
> arlAttachments.Add(.FileName) ' <<< Here's the error... >>>
>
> Dim strFileName() As String = .FileName.Split(New Char()
> {CChar("\")})
> System.Array.Reverse(strFileName)
> lstAttachments.Items.Add(strFileName(0))
> End If
> End With
> End Sub
>
> Private Sub cmdSendMsg_Click(ByVal sender As System.Object, _
> ByVal e As System.EventArgs) Handles cmdSendMsg.Click
>
> Const cstrTitre As String = "Sofica"
>
> If Me.txtTo.Text = "" Then
> Dim strMsg As String = "The field 'To' must be present."
> MessageBox.Show(strMsg, cstrTitre, MessageBoxButtons.OK, _
> MessageBoxIcon.Exclamation)
> Exit Sub
> End If
>
> Dim oApp As New Outlook.Application()
>
> Dim oMsg As Outlook.MailItem =
> oApp.CreateItem(Outlook.OlItemType.olMailItem)
> oMsg.To = Me.txtTo.Text
> oMsg.Subject = Me.txtSubject.Text
> oMsg.Body = Me.txtMsg.Text
>
> If Not IsNothing(arlAttachments) Then
> Dim oAttachment As Outlook.Attachment
> For Each oAttachment In arlAttachments
> oMsg.Attachments.Add(oAttachment) ' <<< Or here... >>>
> Next
> End If
>
> oMsg.Display()
> oMsg.Send()
>
> oMsg = Nothing
> oApp = Nothing
>
> End Sub
>
> End Class
> ' ***** End of code *****
>
> I Use VS.NET 2005 Professional, VB.NET 2005, Outlook 2003 (French version)
> and Windows XP Pro.
> I need some help. Thanks.
> --
> Jac Tremblay
12 Nov 2007Re: Add attachments to message via VB.NET 2005.Sue Mosher [MVP-Outlook]
12 Nov 2007\ Re: Add attachments to message via VB.NET 2005.Jac Tremblay
12 Nov 2007   \ Re: Add attachments to message via VB.NET 2005.Sue Mosher [MVP-Outlook]
13 Nov 2007      \ Re: Add attachments to message via VB.NET 2005.Jac Tremblay
13 Nov 2007         \ Re: Add attachments to message via VB.NET 2005.Sue Mosher [MVP-Outlook]
13 Nov 2007            \ Re: Add attachments to message via VB.NET 2005.Jac Tremblay
All times are in (US) Eastern Daylight Time (GMT -4:00)