Question Retrieve specific attachment from outlook.


New member
Apr 9, 2021
Programming Experience
I'm trying to get a specific file from an e-mail in outlook 2016. i found some code on internet but it attracked all de attachments from all e-mails.
I need to retrieve a specific attachment Lest say "Test.xlsx" to a specific directory Lets say "C:\Temp"
Below is the code i used from internet, it is working but not for my specific file.
I'm using visual studio 2017
Can someone help me with this ??

Public Class Form1 Inherits Form #Region " Windows Form Designer generated code " #End Region Private fiCount As Integer Private fsPath As String = "C:\Temp\" Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click If SaveAttachments(fsPath) Then 'object is created MessageBox.Show(fiCount & " file(s) saved in " & fsPath) Else 'object bombed out MessageBox.Show("Cannot create Outlook object. Check that Outlook is running.") End If End Sub Public Function SaveAttachments(ByVal sPathName As String) As Boolean '************************************************************ ' USAGE: SAVES ATTACHMENTS FROM INBOX TO A DIRECTORY ' PATH NAME MUST HAVE "\" AT THE END ' REQUIRES: OUTLOOK TO BE INSTALLED ON RUNNING MACHINE AND ' A REFERENCE TO THE OUTLOOK OBJECT LIBRARY ' RETURNS: TRUE IF SUCCESSFUL, FALSE OTHERWISE '************************************************************* Dim oOutlook As Outlook.Application Dim oNs As Outlook.NameSpace Dim oFldr As Outlook.MAPIFolder Dim oMessage As Object 'Dim oAttachment As Outlook.Attachment Dim iCtr As Integer Dim iAttachCnt As Integer Dim sFileName As String Try oOutlook = New Outlook.Application oNs = oOutlook.GetNamespace("MAPI") 'get Inbox folder oFldr = oNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) 'browse the folder For Each oMessage In oFldr.Items With oMessage.Attachments iAttachCnt = .Count If iAttachCnt > 0 Then 'if we have attachment For iCtr = 1 To iAttachCnt Try sFileName = .Item(iCtr).FileName.ToString .Item(iCtr).SaveAsFile(sPathName & sFileName) fiCount += 1 Catch ex As Exception 'do nothing End Try Next iCtr End If End With Application.DoEvents() Next oMessage Return True Catch Return False Finally oMessage = Nothing oFldr = Nothing oNs = Nothing oOutlook = Nothing End Try End Function End Class


VB.NET Forum Moderator
Staff member
Dec 17, 2005
Programming Experience
Use an If statement to check sFileName.
Top Bottom