Avatar of Legolas786
Legolas786

asked on 

Save attachment from Outlook folder using vb.net

Hi,

Is it possible to save attachment from an outlook folder to a folder on my computer?

What I am trying to do is
save all attachments that were sent today from X sender with X as the subject in X folder.


I searched expert exchange and came across the following code but it doesnt do anything, doesnt error but doesnt do anything.
Imports Outlook = Microsoft.Office.Interop.Outlook

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
#End Region

    Private fiCount As Integer
    Private fsPath As String = "C:\Temp\Attachments\"
    

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Me.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

Open in new window

Visual Basic.NETMicrosoft Development

Avatar of undefined
Last Comment
Pavel Celba

8/22/2022 - Mon