Saving outlook email attachment?

  • Thread starter Thread starter c_shah
  • Start date Start date
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
 
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
 
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
 
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
 
Back
Top