Solved

Save attachment from Outlook folder using vb.net

Posted on 2014-12-01
11
115 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Simply place CREATEOBJECT instead of the existing line
oOutlook = New Outlook.Application
so
oOutlook = CreateObject("Outlook.Application")
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 13

Expert Comment

by:Jesus Rodriguez
Comment Utility
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
Comment Utility
I've requested that this question be deleted for the following reason:

Got help from elsewhere.
0
 
LVL 41

Expert Comment

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

Accepted Solution

by:
Legolas786 earned 0 total points
Comment Utility
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
Comment Utility
Thanks, so please mark your last post as the solution.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

15 Experts available now in Live!

Get 1:1 Help Now