Automatically save attachment from specific sender

  • Thread starter RichardSchollar
  • Start date
R

RichardSchollar

Hi

I want to automatically save down an attachment from a specified
sender to a specified directory on my computer. I rarely have ever
used Outlook VBA, so I am after some guidance and possibly sample
code.

Having looked at the ThisOutlookSession module in the VBE am I correct
in thinking that I could use the event

Application_NewMailEx(ByVal EntryIDCollection As String)

to check for new mail from a specified contact and if an attachment is
present to save the attachment down to folder X? I would subsequently
like to display a message alert on the desktop.

Will this work in the background? ie I spend most of my time in Excel,
so I would like Outlook to silently monitor incoming email and process
it automatically, just alerting me when the file is saved down to the
directory.

I did consider using Rules, but couldn't see any way to get a macro to
fire from the rule (so this may be down to my ineptitude rather than
it not being possible).

Any help forthcoming is appreciated.

Richard
 
S

Sue Mosher [MVP-Outlook]

The page at http://www.outlookcode.com/article.aspx?id=62 shows how to run an Outlook VBA procedure from a rule, as well as how to use NewMailEx to process incoming messages. Both work in the background, as long as Outlook is running.

These samples show various ways to save attachments:

http://www.fontstuff.com/outlook/oltut01.htm
http://www.outlookcode.com/codedetail.aspx?id=70
http://www.outlookcode.com/codedetail.aspx?id=866
http://www.outlookcode.com/codedetail.aspx?id=1494
http://www.slovaktech.com/code_samples.htm#StripAttachments
 
R

RichardSchollar

Sue

Thanks very much for this - somebody earlier today pointed me to your
site and I have been having a great time trawling the code. I have
also just ordered your Outlook 2007 Programming book from Amazon.

Thanks again.

Richard
 
K

Ken Slovak - [MVP - Outlook]

I'd go with a macro called by a rule myself. That way you can set the rule
to fire when emails come in from specific senders that have one or more
attachments.

For a macro called by a rule the format is pretty simple. You set the rule
to run a script, which is your macro. The macro has a format of being Public
and having only 1 input argument, the MailItem that triggered the rule. So
the macro would look something like this:

Public Sub myRuleMacro(Item As Outlook.MailItem)
' code here
End Sub

For saving the attachment you can modify the attachment saving code at
http://www.slovaktech.com/code_samples.htm#StripAttachments, which saves all
attachments in the currently selected items in a folder. You can remove the
loop and use the Item passed to you as the handle for the item to remove
attachments from.
 
R

RichardSchollar

Ken

I appreciate the help - I will be looking to implement this as soon as
I can. Thank you very much!

Richard


I'd go with a macro called by a rule myself. That way you can set the rule
to fire when emails come in from specific senders that have one or more
attachments.

For a macro called by a rule the format is pretty simple. You set the rule
to run a script, which is your macro. The macro has a format of being Public
and having only 1 input argument, the MailItem that triggered the rule. So
the macro would look something like this:

Public Sub myRuleMacro(Item As Outlook.MailItem)
    ' code here
End Sub

For saving the attachment you can modify the attachment saving code athttp://www.slovaktech.com/code_samples.htm#StripAttachments, which saves all
attachments in the currently selected items in a folder. You can remove the
loop and use the Item passed to you as the handle for the item to remove
attachments from.

--
Ken Slovak
[MVP - Outlook]http://www.slovaktech.com
Author: Professional Programming Outlook 2007
Reminder Manager, Extended Reminders, Attachment Optionshttp://www.slovaktech.com/products.htm




I want to automatically save down an attachment from a specified
sender to a specified directory on my computer.  I rarely have ever
used Outlook VBA, so I am after some guidance and possibly sample
code.
Having looked at the ThisOutlookSession module in the VBE am I correct
in thinking that I could use the event
Application_NewMailEx(ByVal EntryIDCollection As String)
to check for new mail from a specified contact and if an attachment is
present to save the attachment down to folder X?  I would subsequently
like to display a message alert on the desktop.
Will this work in the background? ie I spend most of my time in Excel,
so I would like Outlook to silently monitor incoming email and process
it automatically, just alerting me when the file is saved down to the
directory.
I did consider using Rules, but couldn't see any way to get a macro to
fire from the rule (so this may be down to my ineptitude rather than
it not being possible).
Any help forthcoming is appreciated.
Richard- Hide quoted text -

- Show quoted text -
 
D

DuanePaulson

RichardSchollar said:
Hi

I want to automatically save down an attachment from a specified
sender to a specified directory on my computer. I rarely have ever
used Outlook VBA, so I am after some guidance and possibly sample
code.

Having looked at the ThisOutlookSession module in the VBE am I correct
in thinking that I could use the event

Application_NewMailEx(ByVal EntryIDCollection As String)

to check for new mail from a specified contact and if an attachment is
present to save the attachment down to folder X? I would subsequently
like to display a message alert on the desktop.

Will this work in the background? ie I spend most of my time in Excel,
so I would like Outlook to silently monitor incoming email and process
it automatically, just alerting me when the file is saved down to the
directory.

I did consider using Rules, but couldn't see any way to get a macro to
fire from the rule (so this may be down to my ineptitude rather than
it not being possible).

Any help forthcoming is appreciated.

Richard

I was faced with the same problem myself, and found the pointers in the
replies very helpful. Here's what I came up with.

'SaveToFolder -- Intended to be called by Outlook Rule.
' Saves all attachments within message to specified folder,
' and appends a date-time stamp to the filename to allow
' for attachments in subsequent emails with duplicate names.
' Received time is used as the date-time stamp.

Sub SaveToFolder(MyMail As MailItem)
Dim strID As String
Dim objNS As Outlook.NameSpace
Dim objMail As Outlook.MailItem
Dim objAtt As Outlook.Attachment
Dim c As Integer
Dim save_name As String
'Place path to sav to on next line. Note that you must include the
final backslash.
Const save_path As String = "C:\Temp\"

strID = MyMail.EntryID
Set objNS = Application.GetNamespace("MAPI")
Set objMail = objNS.GetItemFromID(strID)

If objMail.Attachments.Count > 0 Then
For c = 1 To objMail.Attachments.Count
Set objAtt = objMail.Attachments(c)
save_name = Left(objAtt.FileName, Len(objAtt.FileName) - 4)
save_name = save_name & Format(objMail.ReceivedTime,
"_mm-dd-yyyy_hhmm")
save_name = save_name & Right(objAtt.FileName, 4)
objAtt.SaveAsFile save_path & save_name

Next
End If

Set objAtt = Nothing
Set objMail = Nothing
Set objNS = Nothing
End Sub
 

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