Solved

Save attachment from Outlook folder using vb.net

Posted on 2014-12-01
11
122 Views
Last Modified: 2014-12-06
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

0
Comment
Question by:Legolas786
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
11 Comments
 
LVL 42

Expert Comment

by:pcelba
ID: 40475745
If you are saying " it doesnt do anything" then you have to make sure the code is executed at least... Simply open the source in Visual Studio and set the breakpoint to your  Button1_Click method.

Then you may Start debugging and when the program stops on your breakpoint (after button click) you may execute it line by line and watch what are values processed by the code.

Very easy. What you do yourself you remember forever.

For more advanced changes you may study Outlook object model: http://msdn.microsoft.com/en-us/library/ms268893.aspx
0
 

Author Comment

by:Legolas786
ID: 40475770
Thanks, matter of a fact I do have an error of cannot create outlook object,  check that outlook is running.  But when i go into task manager i can see that the outlook process is running and the application is running (I have it open)
0
 
LVL 42

Expert Comment

by:pcelba
ID: 40475794
Did you set the reference to Outlook as stated here: http://msdn.microsoft.com/en-us/library/bb206737%28v=office.12%29.aspx  ?

Alternatively you could try Late Binding i.e. CreateObject("Outlook.Application")  which work almost everywhere.
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

Author Comment

by:Legolas786
ID: 40476295
hi, yes i did already add the reference, could you please tell me where the createobject line would go?
0
 
LVL 42

Expert Comment

by:pcelba
ID: 40476465
Simply place CREATEOBJECT instead of the existing line
oOutlook = New Outlook.Application
so
oOutlook = CreateObject("Outlook.Application")
0
 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 40477185
Try to Close your Outlook before run the code and try to run the code. It happens when you already has your outlook application open and is trying to create or open the existing one.
0
 

Author Comment

by:Legolas786
ID: 40484487
I've requested that this question be deleted for the following reason:

Got help from elsewhere.
0
 
LVL 42

Expert Comment

by:pcelba
ID: 40484488
I would like to see the final solution from elsewhere before the question deletion.
0
 

Accepted Solution

by:
Legolas786 earned 0 total points
ID: 40484509
well someone else told me to reinstall office, which i dont and now id ont need any help.
0
 
LVL 42

Expert Comment

by:pcelba
ID: 40484533
Thanks, so please mark your last post as the solution.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question