none
Outlook Interop - can't add attachment RRS feed

  • Question

  • Hello,

    we have a problem while trying to add an attachment to an email with Outlook Interop for Visual Basic .NET.

    We are using Visual Studio 2017, Outlook 2016 and Microsoft.Office.Interop.Outlook version 15.0.4797.1003

    This is the code:

                ' Create the Outlook application.
                Dim outlookApp As New Outlook.Application()
                ' Create a new mail item.
                Dim outlookMailItem = DirectCast(outlookApp.CreateItem(OlItemType.olMailItem), MailItem)
                outlookMailItem.HTMLBody = "Test message"
                'Add an attachment.
                Dim displayNameForAttachment = "MyAttachment"
                Dim position As Integer = CInt(outlookMailItem.Body.Length) + 1
                Dim attachType = CInt(OlAttachmentType.olByValue)
                outlookMailItem.Subject = "Test subject"
                If IO.File.Exists(File) Then
                    Dim objattachments As Attachments = outlookMailItem.Attachments
                    Dim attach As Attachment
                    attach = objattachments.Add(File)
                    outlookMailItem.Attachments.Add(attach)
                End If
                outlookMailItem.Display()

    Where "File" is a string with the full path of the file I am trying to attach, in this case as an example "C:\Test\testfile.txt"

    The file exists in my computer, but when I try to add this to the Attachment with the "objattachments.Add" it gives an error: "Invalid file or directory name". I did also try with different files in different folders but I always have the same error. 

    Also, if I remove the attachment and open the email, I am able to drag and drop this "testfile.txt" in the email without any error.

    I did try with different file in different paths but the error keeps showing. The mail will open with the correct subject and body but without attachment.

    I believe this should be the correct way to add attachments. Is there another way? Or do I need to change the string or the format?

    My purpose is to open an email with some text and one (or more) attachment, but without recipient.

    Thanks,
    Luca

    Monday, March 9, 2020 5:27 PM

Answers

  • Hello.

    Thanks for the suggestion, but I got the same error even with the suggestion made by dbasnett, and the link provided by Karen was not working for me since I need to open the email before sending it.

    Anyway, I find the issue: there was a space at the end of the "File" string that was causing the issue. Removing this space seems to work with the correct file path.

     

    Tuesday, March 10, 2020 1:59 PM

All replies

  • Hello,

    Even though this is done in VB.NET the question is more suited to be in the "Outlook for developers" forum, if you like I can move your question there.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, March 9, 2020 5:52 PM
  • Hello Karen,

    Yes, I think you are right. If you can, move this question there.

    Thank you!

    Monday, March 9, 2020 5:58 PM
  • Have you tried it this way?

                            If IO.File.Exists(File) Then
                                outlookMailItem.Attachments.Add(File)
                            End If
    


    Search Documentation

    SerialPort Info

    Multics - An OS ahead of its time.

     "Those who use Application.DoEvents have no idea what it does

        and those who know what it does never use it."    former MSDN User JohnWein

    Monday, March 9, 2020 8:24 PM
  • Hello Karen,

    Yes, I think you are right. If you can, move this question there.

    Thank you!

    Just moved it. See also http://www.systemnetmail.com/faq/3.4.1.aspx

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, March 9, 2020 9:37 PM
  • Hello.

    Thanks for the suggestion, but I got the same error even with the suggestion made by dbasnett, and the link provided by Karen was not working for me since I need to open the email before sending it.

    Anyway, I find the issue: there was a space at the end of the "File" string that was causing the issue. Removing this space seems to work with the correct file path.

     

    Tuesday, March 10, 2020 1:59 PM