Saving attachemtns automatically

F

Frank

Hi,

I'm trying to implement a solution that saves all my attachments in a folder
automatically.
I found the code below that is a good start.

However, I have some questions:

1.) How can I make a simple method like the one below visible in the outlook
menu or toolbar?

2.) I would like to run a "rule" and execute a script when I receive an
email with attachment. I know how to setup the rule. But I don't know how to
structure the script to receive the proper input value and execute SaveAs
only for this email

Any help would be appreciated.



Private Sub SaveAttachements()

Const olFolderInbox = 6

Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)

Set colItems = objFolder.Items

Set objFSO = CreateObject("Scripting.FileSystemObject")

On Error Resume Next

For Each objMessage In colItems
intCount = objMessage.Attachments.Count
If intCount > 0 Then
For i = 1 To intCount
strTempFile = objFSO.GetTempName
objMessage.Attachments.Item(i).SaveAsFile
"C:\mailarchive\attachments\" & strTempFile & "_" & _
objMessage.Attachments.Item(i).FileName
Next
End If
Next


End Sub
 
S

Sue Mosher [MVP-Outlook]

1) View | Toolbars | Customize, then drag your macro from the list of commands to the desired toolbar.

2) In the procedure for a "run a script" rule action, the item being processed is passed as an argument :


Sub RunAScriptRuleRoutine(MyMail As MailItem)
Dim strID As String
Dim olNS As Outlook.NameSpace
Dim msg As Outlook.MailItem
Dim rply as Outlook.MailItem

strID = MyMail.EntryID
Set olNS = Application.GetNamespace("MAPI")
Set msg = olNS.GetItemFromID(strID)
' do stuff with msg, e.g.
msg.Attachments(1).SaveAsFile <some file name>

Set msg = Nothing
Set rply = Nothing
Set olNS = Nothing
End Sub
 
F

Frank

Hi,

Thanks for the fast answers.

About 1.) How do I need to declare my Sub so that it shows up in the command
list?

A new question:
Is there a way to check the date of the attachment item? I would like to
overwrite older attachments.


1) View | Toolbars | Customize, then drag your macro from the list of
commands to the desired toolbar.

2) In the procedure for a "run a script" rule action, the item being
processed is passed as an argument :


Sub RunAScriptRuleRoutine(MyMail As MailItem)
Dim strID As String
Dim olNS As Outlook.NameSpace
Dim msg As Outlook.MailItem
Dim rply as Outlook.MailItem

strID = MyMail.EntryID
Set olNS = Application.GetNamespace("MAPI")
Set msg = olNS.GetItemFromID(strID)
' do stuff with msg, e.g.
msg.Attachments(1).SaveAsFile <some file name>

Set msg = Nothing
Set rply = Nothing
Set olNS = Nothing
End Sub
 
S

Sue Mosher [MVP-Outlook]

1) As a macro, public with no arguments.

2) "Older" compared with what? You can work with the attachment itself only after it has been saved.
 
F

Frank

Hi,

I got almost everything to work.

I would like to overwrite the file in my local folder only if the latest
attachment file is newer than the one already stored. Oftentimes, I receive
iterations of the same file name.

Currently, I'm using a randomizer:

Set objFSO = CreateObject("Scripting.FileSystemObject")
objMessage.Attachments.Item(i).SaveAsFile "C:\mailarchive\attachments\" &
strTempFile & "_" & _
objMessage.Attachments.Item(i).FileName

I would like to modify to:
IF mylocalfile.date < attachement.date THEN
SaveAsFile




1) As a macro, public with no arguments.

2) "Older" compared with what? You can work with the attachment itself only
after it has been saved.
 
S

Sue Mosher [MVP-Outlook]

An attachment in a mail message has no date properties. The only date information available would be the date properties associated with the parent message.
 
D

Dmitry Streblechenko

Actually Exchange (when a message is sent between two mailboxes) preserves
PR_LAST_MODIFICATION_TIME and PR_CREATION_TIME on attachments. When a
message is received over POP3, these properties are gone obviously.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool

An attachment in a mail message has no date properties. The only date
information available would be the date properties associated with the
parent message.
 
S

Sue Mosher [MVP-Outlook]

That would explain why I don't see them, since I was looking at POP3 data. Are the values related to when the attachment itself was originally created or modified as a file or do they reflect when the message itself was created/modified?

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
 
D

Dmitry Streblechenko

Outlook sets them to the real file system dates (ftCreationTime and
ftLastWriteTime).
Even under POP3, you should be able to see these properties on the messages
in your Sent Items folder.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool

That would explain why I don't see them, since I was looking at POP3 data.
Are the values related to when the attachment itself was originally created
or modified as a file or do they reflect when the message itself was
created/modified?

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
 
S

Sue Mosher [MVP-Outlook]

You would need to use Outlook 2007, CDO 1.21, or Redemption (http://www.dimastr.com/redemption) because those are hidden MAPI properties. But first you might want to use Dmitry's Outlook Spy tool or the free MFCMAPI.exe tool from Microsoft to see if they're available on your data.
 
D

Dmitry Streblechenko

Redemption also explicitly exposes them as Attachment/RDOAttachment.
LastModificationTime / CreationTime properties.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool

You would need to use Outlook 2007, CDO 1.21, or Redemption
(http://www.dimastr.com/redemption) because those are hidden MAPI
properties. But first you might want to use Dmitry's Outlook Spy tool or the
free MFCMAPI.exe tool from Microsoft to see if they're available on your
data.
 
F

Frank

That is probably not an option for me as I'm programming this on a corporate
load of outlook and OS?
 
D

Dmitry Streblechenko

Then your only options are
1. Outlook 2007 (Attachment.PropertyAccessor)
2. Extended MAPI (C++ or Delphi only)
3. CDO 1.21 (Attachment.Fields) - CDO 1.21 is an optional component in
Outlook 2002/2003, and is not installed at all by Outlook 2007.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 

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