Function AttachmentsMove()
' This Outlook macro checks a the Outlook Inbox for messages
' with attached files (of any type) and saves them to disk.
' NOTE: make sure the specified save folder exists before
' running the macro.
On Error GoTo GetAttachments_err
' Declare variables
Dim myolApp As New Outlook.Application
Dim ns As NameSpace
Dim Inbox As MAPIFolder
Dim Item As Object
Dim Atmt As Attachment
Dim FileName As String
Dim I As Integer
Dim Reports As MAPIFolder
Dim blnFound As Boolean
Set myolApp = CreateObject("Outlook.Application")
Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
Set Reports = ns.Folders("Personal Folders").Folders("Call_Reports")
I = 0
' Check Inbox for messages and exit of none found
If Inbox.Items.Count = 0 Then
MsgBox "There are no messages in the Inbox.", vbInformation, _
"Nothing Found"
Exit Function
End If
blnFound = False
' Check each message for attachments
For Each Item In Inbox.Items
' Save any attachments found
For Each Atmt In Item.Attachments
' This path must exist! Change folder name as necessary.
If Atmt.FileName = "Calls by Extension Master List.xls" Then
blnFound = True
Atmt.SaveAsFile "P:\databases\downloads\extensionstats\" & Atmt.FileName
I = I + 1
Item.Move Reports
End If
Next Atmt
If I = 1 Then
Exit For
End If
Next Item
If Not blnFound Then
MsgBox "Master Call List Summary email not found in Inbox"
' if you want the code to stop right here,
Stop
' if you want the code to stop running,
'End
End If
' Clear memory
GetAttachments_exit:
Set Atmt = Nothing
Set Item = Nothing
Set ns = Nothing
Exit Function
' Handle errors
GetAttachments_err:
MsgBox "Couldn't get into Outlook."
Stop
'& vbCrLf & "Please note and report the following information." _
'& vbCrLf & "Macro Name: GetAttachments" _
'& vbCrLf & "Error Number: " & Err.Number _
'& vbCrLf & "Error Description: " & Err.Description _
, vbCritical, "Error!"
Resume GetAttachments_exit
End Function
|