Solved

Save attachment from Outlook folder using vb.net

Posted on 2014-12-01
11
117 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
  • 5
  • 4
11 Comments
 
LVL 41

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 41

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
 

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 41

Expert Comment

by:pcelba
ID: 40476465
Simply place CREATEOBJECT instead of the existing line
oOutlook = New Outlook.Application
so
oOutlook = CreateObject("Outlook.Application")
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 41

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 41

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net - CSV to Oracle table 4 52
Connect to a database from Excel using JDBC instead of ODBC 3 47
Copy/Clone an object. 9 29
Get Top Visible Row of Datagridview 6 27
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now