Home All Groups Group Topic Archive Search About

Saving outlook email attachment?

Author
25 Apr 2006 7:31 PM
c_shah
how to save outlook email attachments using VB.net?

Author
25 Apr 2006 8:40 PM
krgatez
Public Sub GetAttachments()
              ' Declare variables
        Dim ns As [NameSpace]
        Dim Inbox As MAPIFolder
        Dim Item As MailItem
        Dim SubFolder As MAPIFolder
        Dim Atmt As Attachment
        Dim FileName As String
        Dim olApp As Application

        Dim myRecipient As Recipient
        olApp = CreateObject("Outlook.Application")
        ns = olApp.GetNamespace("MAPI")
        ' Set myRecipient =outlook ns.CreateRecipient("help")
        'myRecipient.Resolve
        Inbox = ns.GetDefaultFolder(OlDefaultFolders.olFolderInbox)
        SubFolder = Inbox.Folders("Inbox")
        ' Check Inbox for messages and exit of none found
        If SubFolder.Items.Count = 0 Then
            Exit Sub
        End If
        ' Check each message for attachments
        For Each Item In SubFolder.Items
            ' Save attachments found
                  For Each Atmt In Item.Attachments
                        FileName = "h:\Email Attachments\" &
Atmt.FileName
                        Atmt.SaveAsFile(FileName)
                       End If
                Next Atmt
            End If
         Next Item

        Atmt = Nothing
        Item = Nothing
        ns = Nothing
       End Sub

c_shah wrote:
Show quoteHide quote
> how to save outlook email attachments using VB.net?
Author
25 Apr 2006 9:22 PM
c_shah
I am getting compilation error
this is my code


Imports Outlook = Microsoft.Office.Interop.Outlook

  ' Create Outlook Application.
        Dim objApp As Outlook.Application = New Outlook.Application

        'Get Mapi NameSpace
        Dim objNS As Outlook.NameSpace = objApp.GetNamespace("MAPI")


        ' Get Messages collection of Inbox.
        Dim objInbox As Outlook.MAPIFolder =
objNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
        Dim objItems As Outlook.Items = objInbox.Items

        ' Get unread e-mail messages.
        objItems = objItems.Restrict("[Unread] = true")

        Dim item As Outlook.MailItem

        Dim i As Integer

        'Dim Item As Object

        For i = 1 To objItems.Count
            '    MessageBox.Show(objItems.Item(i).Subject)
            '    MessageBox.Show(objItems.Item(i).Body)

            Dim objAttachment As Outlook.Attachment
            For Each objAttachment In objItems.Item(i)

                Dim filename As String
                filename = "C:\Temp\" + objAttachment.FileName
                objAttachment.SaveAsFile(filename)
            Next objAttachment


        Next
Author
26 Apr 2006 5:48 AM
Cor Ligthert [MVP]
C_shah,

>I am getting compilation error this is my code
>
Probably you did not set a reference or something like that.

Cor
Author
26 Apr 2006 2:36 PM
c_shah
It is working now.
Here is my complete code

I am getting a security warning from outlook "a program is trying to
access email address from outlook" how to get rid of that message?

Also, instead of "Unread items" from the inbox  I would like to use
selected(highlighted) items from the inbox any way to do this

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load


        ' Create Outlook Application.
        Dim objApp As Outlook.Application = New Outlook.Application

        'Get Mapi NameSpace
        Dim objNS As Outlook.NameSpace = objApp.GetNamespace("MAPI")

        objNS.Logon("GA\Cshah", Missing.Value, False, True)

        ' Get Messages collection of Inbox.
        Dim objInbox As Outlook.MAPIFolder =
objNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
        Dim objItems As Outlook.Items = objInbox.Items

        ' Get unread e-mail messages. (Collection)
        objItems = objItems.Restrict("[UnRead] = true")

        ' objItems = objItems.Restrict(

        'Mail Item
        Dim oMsg As Outlook.MailItem

        For Each oMsg In objItems

            Dim objAttachment As Outlook.Attachment
            For Each objAttachment In oMsg.Attachments

                Dim filename As String
                filename = "C:\Temp\" + objAttachment.FileName
                objAttachment.SaveAsFile(filename)
            Next objAttachment


        Next

        Label1.Text = "Attachments are saved"
    End Sub
Author
26 Apr 2006 4:18 PM
Cor Ligthert [MVP]
C_Shah,

Have a look in this thread there is no 1 to 1 answer, so maybe you find it
in there.

http://groups.google.com/group/microsoft.public.dotnet.languages.vb/browse_frm/thread/507e83e2fd19337c/15a6683ae027bb23#15a6683ae027bb23

I hope this helps,

Cor

Show quoteHide quote
"c_shah" <shah.chi***@netzero.net> schreef in bericht
news:1146062168.533079.199590@g10g2000cwb.googlegroups.com...
> It is working now.
> Here is my complete code
>
> I am getting a security warning from outlook "a program is trying to
> access email address from outlook" how to get rid of that message?
>
> Also, instead of "Unread items" from the inbox  I would like to use
> selected(highlighted) items from the inbox any way to do this
>
> Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
>
>
>        ' Create Outlook Application.
>        Dim objApp As Outlook.Application = New Outlook.Application
>
>        'Get Mapi NameSpace
>        Dim objNS As Outlook.NameSpace = objApp.GetNamespace("MAPI")
>
>        objNS.Logon("GA\Cshah", Missing.Value, False, True)
>
>        ' Get Messages collection of Inbox.
>        Dim objInbox As Outlook.MAPIFolder =
> objNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
>        Dim objItems As Outlook.Items = objInbox.Items
>
>        ' Get unread e-mail messages. (Collection)
>        objItems = objItems.Restrict("[UnRead] = true")
>
>        ' objItems = objItems.Restrict(
>
>        'Mail Item
>        Dim oMsg As Outlook.MailItem
>
>        For Each oMsg In objItems
>
>            Dim objAttachment As Outlook.Attachment
>            For Each objAttachment In oMsg.Attachments
>
>                Dim filename As String
>                filename = "C:\Temp\" + objAttachment.FileName
>                objAttachment.SaveAsFile(filename)
>            Next objAttachment
>
>
>        Next
>
>        Label1.Text = "Attachments are saved"
>    End Sub
>
Author
26 Apr 2006 5:05 PM
c_shah
Thank you Cor.

Also, anyone know a property that exposes currently selected emails
from the outlook? So instead of "Unread items" from the inbox  I would
like to read selected(highlighted) items from the inbox