Solved

Save attachment from Outlook folder using vb.net

Posted on 2014-12-01
11
121 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

734 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