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
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
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
"RichardSchollar" <Richard...@googlemail.com> wrote in message news:d32c4b57-3f81-4dc2...@y5g2000hsf.googlegroups.com...
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
On 5 Feb, 14:59, "Sue Mosher [MVP-Outlook]" <sue...@outlookcode.com>
wrote:
> The page athttp://www.outlookcode.com/article.aspx?id=62shows 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:
>
>
> --
> Sue Mosher, Outlook MVP
> Author of Microsoft Outlook 2007 Programming:
> Jumpstart for Power Users and Administrators
> http://www.outlookcode.com/article.aspx?id=54
>
>
>
> "RichardSchollar" <RichardSchol...@googlemail.com> wrote in messagenews:d32c4b57-3f81-4dc2...@y5g2000hsf.googlegroups.com...
> > 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- Hide quoted text -
>
> - Show quoted text -
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.
--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm
"RichardSchollar" <Richard...@googlemail.com> wrote in message
news:d32c4b57-3f81-4dc2...@y5g2000hsf.googlegroups.com...
I appreciate the help - I will be looking to implement this as soon as
I can. Thank you very much!
Richard
On 5 Feb, 15:59, "Ken Slovak - [MVP - Outlook]" <kenslo...@mvps.org>
wrote:
> 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
>
> "RichardSchollar" <RichardSchol...@googlemail.com> wrote in message
>
> news:d32c4b57-3f81-4dc2...@y5g2000hsf.googlegroups.com...
>
>
>
> > 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.
>
"RichardSchollar" wrote:
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