Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Save attachment from Outlook folder using vb.net

Posted on 2014-12-01
11
Medium Priority
?
126 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
10 Comments
 
LVL 43

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 43

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 43

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 43

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 43

Expert Comment

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
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…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

877 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