Computing.Net > Forums > Programming > Outlook Macro: Moving Messages

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Outlook Macro: Moving Messages

Reply to Message Icon

Name: mking (by mkingrey)
Date: January 20, 2006 at 11:36:29 Pacific
OS: XP Pro
CPU/Ram: Pent 866/128
Comment:

I am trying to write a macro in Outlook that saves all attachments from messages with .CVS file extensions, and then moves all files with a specific Subject line to a subfolder of the inbox. It works for the most part, but whenever I get to the part where the files are moved from the inbox to the subfolder only 1/2 of the files are moved, regardless of the number of files in my inbox with that Subject Line...Here is my code:

Option Explicit

Private Sub Application_Startup()
'Attribute VB_Name = "GetEmailAttachments"


' 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 ns As NameSpace
Dim Inbox As MAPIFolder
Dim Item As Object
Dim Atmt As Attachment
Dim FileName As String
Dim i As Integer
Dim ItemSubject As String
Dim i2 As Integer
Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
i = 0
i2 = 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 Sub
End If
' Check each message for attachments
For Each Item In Inbox.Items

' Save any attachments found
For Each Atmt In Item.Attachments
If Right(Atmt.FileName, 3) = "csv" Then
' This path must exist! Change folder name as necessary.
FileName = "C:\Email Attachments\" & Atmt.FileName
Atmt.SaveAsFile FileName
i = i + 1
End If

Next Atmt

'If Item.Subject = "Monday Morning MDB Audit Reports" Then
'Item.Move (Inbox.Folders("Audit Reports"))
'End If
Next Item



For Each Item In Inbox.Items

' Move files to subfolder
ItemSubject = Item.Subject
If ItemSubject = "Monday Morning MDB Audit Reports" Then
Item.Move (Inbox.Folders("Audit Reports"))
i2 = i2 + 1
End If
Next Item

' Show summary message
If i > 0 Then
MsgBox "I found " & i & " attached files" & " moved " & i2 & " files." _
& vbCrLf & "I have saved them into the C:\Email Attachments folder." _
& vbCrLf & vbCrLf & "Have a nice day.", vbInformation, "Finished!"
Else
MsgBox "I didn't find any attached files in your mail.", vbInformation, "Finished!"
End If

' Clear memory
GetAttachments_exit:
Set Atmt = Nothing
Set Item = Nothing
Set ns = Nothing
Exit Sub
' Handle errors
GetAttachments_err:
MsgBox "An unexpected error has occurred." _
& 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 Sub


I'm puzzled please help!!!
Thanks, M



Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Outlook Macro: Moving Messages

Moving Messages based on content www.computing.net/answers/programming/moving-messages-based-on-content/15376.html

Custom Outlook Macro www.computing.net/answers/programming/custom-outlook-macro/3704.html

MS Outlook macro www.computing.net/answers/programming/ms-outlook-macro/8622.html