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
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
' Get Messages collection of Inbox.
Dim objInbox As Outlook.MAPIFolder =
objNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
Dim objItems As Outlook.Items = objInbox.Items
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
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.