Delete/Move Attachments

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Ok, so my archiving idea isn't going to solve all my space concerns. Here's
another idea I need help with.

I would like to see the VBA code that would crawl through all my folders and
do one of two things:
1. If the message is from me, delete any attachments
2. If the message isn't from me, move the attachments to a folder and then
delete them.
I would like to do this for every message older than say 3 months.

Thanks in advance for the help.
 
Hi Gregg,

I suppose iterating through collections is not a problem for you, is it?

For each message you can compare the SenderName property with your
(display) name.

If the message is from yourself iterate through the Attachments
collection and call Attachment.Delete, otherwise call Attachment.SaveAs
and then .Delete.

To determine the age you can use the ReceivedTime property and DateAdd
function, e.g.:

if dateadd("m",message.ReceivedTime,date)>3 then
' match
endif
 
Actually my programming is pretty rusty, I've done some simple things in VBA
in Word, but nothing in Outlook...
 
Hi Gregg,

here is a sample for a recursive iteration through the folders:

' <Sample: Loop through the Outlook folders hierarchy for handling _
each folder item>
Public Sub HandleAllItems(oRoot As Outlook.NameSpace)
LoopFolders oRoot.Folders
End Sub

Private Sub LoopFolders(oFolders As Outlook.Folders)
Dim oFld As Outlook.MAPIFolder

' Loop through all folders.
For Each oFld In oFolders
' Loop through folder items
LoopItems oFld.Items
' Call this function again for going _
deeper into the object hierarchy.
LoopFolders oFld.Folders
Next
End Sub

Private Sub LoopItems(oItems As Outlook.Items)
Dim obj As Object

For Each obj In oItems
Select Case obj.Class
Case olMail
HandleMailItem obj
End Select
Next
End Sub

Private Sub HandleMailItem(oItem As Outlook.MailItem)
' Do s.th. with the object.
End Sub
' </Sample>


Call HandleAllItems Application.Session for starting.

This sample just iterates through all items in all folders. Add your
validation in the function "HandleMailItem". You need to understand the
mechanismn of the loop, then you can easily copy and modify it for the
loop through the Attachments.

In my former post I´ve named four you a few necessary or helpful
keywords, like SenderName or Attachment.Delete etc. Please open the
Object Browser (F2), switch from <All Libraries> to <Outlook> and search
for this keywords. Then select them and press F1. The VBA help provides
you a lot of good samples.
 
Thank you Michael!!!

Michael Bauer said:
Hi Gregg,

here is a sample for a recursive iteration through the folders:

' <Sample: Loop through the Outlook folders hierarchy for handling _
each folder item>
Public Sub HandleAllItems(oRoot As Outlook.NameSpace)
LoopFolders oRoot.Folders
End Sub

Private Sub LoopFolders(oFolders As Outlook.Folders)
Dim oFld As Outlook.MAPIFolder

' Loop through all folders.
For Each oFld In oFolders
' Loop through folder items
LoopItems oFld.Items
' Call this function again for going _
deeper into the object hierarchy.
LoopFolders oFld.Folders
Next
End Sub

Private Sub LoopItems(oItems As Outlook.Items)
Dim obj As Object

For Each obj In oItems
Select Case obj.Class
Case olMail
HandleMailItem obj
End Select
Next
End Sub

Private Sub HandleMailItem(oItem As Outlook.MailItem)
' Do s.th. with the object.
End Sub
' </Sample>


Call HandleAllItems Application.Session for starting.

This sample just iterates through all items in all folders. Add your
validation in the function "HandleMailItem". You need to understand the
mechanismn of the loop, then you can easily copy and modify it for the
loop through the Attachments.

In my former post I´ve named four you a few necessary or helpful
keywords, like SenderName or Attachment.Delete etc. Please open the
Object Browser (F2), switch from <All Libraries> to <Outlook> and search
for this keywords. Then select them and press F1. The VBA help provides
you a lot of good samples.
 
Back
Top