|
-
Jan 30th, 2010, 04:38 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Outlook02 Saving attachments using date received
Hi all. (Posted also to MR Excel\General forums with no response so far. I have googled & searched forums.)
I am trying to save attachments using the date sent/recieved in the format as YYMMDD as the attachment is saved as Inv# which is not very useful to me.
The problem I have is that the code is throwing up something weird. for a start it shows the same "YYMMDD" (991229) for all the msgs with attach's. Secondly there is no message sent on 29/12/99 (or 2009) in the business folder.
When I use the debugger with locals watch window the sDate variable comes up with 29/12/1899 ( therefore aDate = 991229) & stays that way thru the loop.
Code:
: sDate : #29/12/1899# : Date
(This is pasted from the watch window.)
Should I use something like "i = 1 to count msgs with attachments" (abbreviated of course) to loop thru the msg & therefore getting date sent for ech msg? (This of course does not account for the date error of 1899?????)
My system is formatted as dd/mm/yyyy with each of my programs date preferences formatted independently (under program settings or options etc) according to my needs.
Code:
Sub SaveOAtts()
'30/1/10 Updated with date option in attach Fname - wip
'29/1/10
'http://www.your-save-time-and-improve-quality-technologies-online-resource.com/
'save-attachments-from-outlook-using.html
Dim ns As NameSpace
Dim fld2SaveAtt As MAPIFolder
Dim MailItem As Object
Dim Att As Attachment
Dim APath As String, FileName As String
Dim sn As String
Dim sDate As Date 'Email sent
Dim aDate As String 'Attachment save date
Dim intFiles As Integer
On Error GoTo HandleError
APath = "C:\Attachments\"
Set ns = GetNamespace("MAPI")
Set fld2SaveAtt = ns.GetDefaultFolder(olFolderInbox).Folders("Business")
intFiles = 0
If fld2SaveAtt.Items.Count = 0 Then
MsgBox "There were no messages found in your Inbox."
Exit Sub 'there are no messages, so Exit the Sub
End If
'Loop through Mail Items
For Each MailItem In fld2SaveAtt.Items
sn = MailItem.SenderName
'Loop through any attachments
For Each Att In MailItem.Attachments
If sn = "[email protected]" Then
sDate = MailItem.Sent
aDate = Format(sDate, "yymmdd")
FileName = Trim(Att.FileName)
FileName = aDate & Att.FileName
Att.SaveAsFile APath & FileName
intFiles = intFiles + 1
Else
End If
Next
Next
' Show summary message
If intFiles > 0 Then
MsgBox intFiles & " attachments were saved to " ^ _
"C:\Attachments."
Else
MsgBox "No attachments were found"
End If
Set Att = Nothing
Set MailItem = Nothing
Set ns = Nothing
Exit Sub
HandleError:
MsgBox "Error: " & Err.Number & vbCrLf & _
"Description: " & Err.Description & vbCrLf & _
"The file's name is " & FileName
'This not required for this procedure intFiles = intFiles - 1
Resume Next 'Continue saving attachments
End sub
Lionel DownUnda (NZ)
XP Prof SP3, Office 2002 SP3
If the universe is expanding what is it expanding into?
-
Jan 30th, 2010, 04:54 PM
#2
Re: Outlook02 Saving attachments using date received
look in the locals window, check mailitem variable to see which property holds the date you want
menu > view > locals window
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jan 30th, 2010, 07:53 PM
#3
Thread Starter
Addicted Member
Re: Outlook02 Saving attachments using date received
Thanks Pete. I have resolved it. Using the Obj Browser Lib under Outlook, MailItem I found the the correct term was "SentOn" so my line looks like this now
Code:
sDate = MailItem.SentOn
And it works
Lionel DownUnda (NZ)
XP Prof SP3, Office 2002 SP3
If the universe is expanding what is it expanding into?
-
Jan 30th, 2010, 10:33 PM
#4
Re: [RESOLVED] Outlook02 Saving attachments using date received
i guessed it would be something like that
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|