copy attachments for mail that arrives on a specific date

G

Guest

Hello,
I am able to write code (with the help of Martin Green's website) that can
copy attachments from the inbox or pst folder and put a copy of the
attachments on my network drive. However I would like to take it one step
further and have the VBA code look at the items in the folder and only copy
those attachments for mail that arrived on a specific date. For example copy
attachments for mail that arrive today or yesterday, or any specific date. I
looked in the help but I did not get the answer I was looking for, here is a
copy of the code.

Sub mailtest()
Dim ns As NameSpace
Dim mainFolder As MAPIFolder
Dim SubFolder As MAPIFolder
Dim SubFolder2 As MAPIFolder
Dim Item As Object
Dim Atmt As Attachment
Dim FileName As String

Dim i As Integer

Set ns = GetNamespace("MAPI")
Set mainFolder = ns.Folders("Personal Folders")
Set SubFolder = mainFolder.Folders("Saved Mail")
Set SubFolder2 = SubFolder.Folders("Test")
i = 0





For Each Item In SubFolder2.Items
For Each Atmt In Item.Attachments
FileName = "H:\Karen W\APC Aide Staff\Email Attachments\" & Atmt.FileName
Atmt.SaveAsFile FileName

i = i + 1

Next Atmt
Next Item

End If
End Sub

Any help would be appreciate.

Karen
 
G

Guest

Thanks Michael,
I tried that by putting in an if statement but I kept getting an error.

If item.receivedTime=Date() then
rest of code

End If


Since I defined the mail item as an object it did not give me the option
for received time, any suggestions????? Should I redefine. Thanks aplenty.

Karen
 
G

Guest

Michael,
The error message I am receiving is a run time error 91
"Object variable or with block variable not set"
The debugger points to the If Statement block. Should I set the object
variable????
Thanks
Karen
 
G

Guest

Michael,
Thanks so much for your help. I had to make a modification to the code
because the Received time property was read only and I could not set it to
specific parameter. I followed your advice on the For Each loop and I got it
to work. Here is the final code for any one who may be interested.

Sub mailAttachments()


Set ns = GetNamespace("MAPI")
Set mainFolder = ns.Folders("foldername")
Set SubFolder = mainFolder.Folders("subfoldername")
Set SubFolder2 = SubFolder.Folders("subfoldername")

' Get the number of items in the folder.
NumItems = SubFolder2.Items.Count
' Set MyItem to the collection of items in the folder.
Set myItems = SubFolder2.Items


'Format for today's date or other date
strDate2 = Format(Date, "mm/dd/yyyy")

' Loop through all of the items in the folder.
For i = 1 To NumItems
If Format(myItems(i).ReceivedTime, "mm/dd/yyyy") = strDate2 Then
'Copy attachments
For Each Atmt In myItems(i).Attachments
FileName = "C:pathname\" & Atmt.FileName
Atmt.SaveAsFile FileName

i = i + 1

Next Atmt
End If
Next i

End Sub
 
G

Guest

One minor correction
change made below,
Had two references to the same integer therefore it was only reading one
item in the collection.
should be Dim s as integer
and Dim i as integer
 

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.

Ask a Question

Top