How to save attachment automatically in Outlook 2003?

J

joannele71

Hi
I am trying to find a macro to save the attachment automatically every time
I receive emails from the same person. The following is my macro. I have
set up the rule to run the script (macro) when the condition is met. I
receive three different emails that meet the condition and they are all
coming at about the same time. However, the script doesn't work at all and I
got the error message "rule operation failed". Sometimes, it works for one
email and sometimes, it doesn't work at all. Please help!! Thanks in
advance.

Or if you have a better way to do this, please let me know.

Sub SaveAttachment(Item As Outlook.MailItem)
Dim myOlApp As Outlook.Application
Dim myInspector As Outlook.Inspector
Dim myItem As Outlook.MailItem
Dim myAttachments As Outlook.Attachments

Set myItem = Application.Session.GetDefaultFolder(olFolderInbox).Items(1)
myItem.Display

Set myOlApp = CreateObject("Outlook.Application")
Set myInspector = myOlApp.ActiveInspector



If Not TypeName(myInspector) = "Nothing" Then
If TypeName(myInspector.CurrentItem) = "MailItem" Then
Set myItem = myInspector.CurrentItem
Set myAttachments = myItem.Attachments

myAttachments.Item(1).SaveAsFile "c:\" & _
myAttachments.Item(1).DisplayName
myItem.Close olDiscard
End If
Else
' MsgBox "The item is of the wrong type."
myItem.Close olDiscard
End If

End Sub
 
S

Sue Mosher [MVP-Outlook]

The MailItem object that you should be working with is Item, the item passed as a parameter to the event handler, not myItem. You do not need to display it. Just work with its Attachments collection:

Set myAttachments = Item.Attachments

etc.


--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
J

joannele71

Hi Sue,

Thank you for your help. After making the changes as per your suggestion,
it worked well when I did my testing. My testing was continuously sending 3
emails to myself and the script was able to save the attachment to the c:\.
However, I got the error message "rule operation failed" when it comes to the
real situation. Do you know what should I do? and why?

Thanks
 
S

Sue Mosher [MVP-Outlook]

What is the difference between "the real situation" and your tests?

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
J

joannele71

I can't think of any differences except the emails are sending by someone
else and I may receive some other emails at the same time.

I receive the 3 emails daily and they come in one after they other within 2
minutes.

thanks
 
J

Jason B

I have played around with this code a little bit and it seems to execute
within but it doesn't give a dialog bix where I can select a location and
file name nor can I find a directory where the attachments are being saved.
Is there a default location I am overlooking within the code?
 
S

Sue Mosher [MVP-Outlook]

Look at the SaveAs statement. Joannele71's original code uses C:\ as the hard-coded location/

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
J

Jason B

Upon re-reading the original post it appears that she is trying to save the
attachment only if it is from a particular person. I am not fluent in VB at
all for the most part, is there a way to cut out some of the parameters and
make this universal to all incoming emails?
 
S

Sue Mosher [MVP-Outlook]

Her code is not a very good example of using a "run a script" rule to run code because it doesn't use the item that the procedure passes as an argument. This is a more correct structure:

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

strID = MyMail.EntryID
Set olNS = Application.GetNamespace("MAPI")
Set msg = olNS.GetItemFromID(strID)
' put code to process attachments here, e.g.
Debug.Print msg.Attachments.Count

Set msg = Nothing
Set olNS = Nothing
End Sub

These samples show various ways to save attachments that you could incorporate into the basic structure and run with no conditions:

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

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
J

joannele71

Hi Sue,

I am not fluent in VB either. Can you explain what is meant by "it doesn't
use the item that the procedure passes as an argument."? I am still trying
to fix my problem - the script doesn't work all the times.

Thanks
 
S

Sue Mosher [MVP-Outlook]

This is an example of a procedure declaration:

Sub RunAScriptRuleRoutine(MyMail As MailItem)

The latter part -- MyMail As MailItem -- is the argument. There are three important things you need to know about that MyMail object:

-- MyMail is a MailItem object, in other words, an Outlook message
-- The code inside the RunAScriptRuleRoutine procedure can act on that MyMail object.
-- If you're running this procedure as part of a "run a script" rule action, the MyMail object represents the actual message that fired the rule.

Now apply that information to the declaration for joannele71's original procedure:

Sub SaveAttachment(Item As Outlook.MailItem)

See the parallel? Applying the points above to joannele71's procedure, the variable name for the message that fired the rule is Item. But joannele71's code completely ignores that very valuable Item object and does nothing with it. Instead, it tries to operate on the currently open item. In other words, joannele71's rule/code says -- whenever a new message comes in from this person, ignore that message and instead try to strip attachments from the message I currently have open (if any). Not very useful, is it?

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
J

joannele71

Thank you very much Sue. Your explanation is very clear and helpful.

I am trying to amend my code based on your suggestion. However, due to my
limited knowledge, the code is not working. Here is my code. Can you please
point out my mistakes again?

Sub RunAScriptRuleRoutine(MyMail As MailItem)

Dim myOlApp As Outlook.Application
Dim msg As Outlook.MailItem
Dim myAttachments As Outlook.Attachments

Set msg = MyMail
Set myAttachments = msg.Attachments

msg.Attachments.SaveAsFile "c:\" & _
msg.Attachments.DisplayName


Set msg = Nothing

End Sub

Many thanks
 
S

Sue Mosher [MVP-Outlook]

Attachments is a collection, not an individual attachment. Like most collections, you can process it with a For Each loop.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
R

RussellHltn

I'm trying to set up a Script to save attachments. I'm using a
different one gleaned from elsewhere, but I don't think the script is
running at all since I don't get any message boxes. I know the rule
is running because the rule I've set up to fire the script successfuly
sets a flag on the message. I've tried playing with security, but
still no luck.

Any suggestions?

The script I'm running is below:

Option Explicit

Const SAVEPATH = "C:\Download\Mail Attachments\"
Sub SaveAttachment(Item As Outlook.MailItem)
Dim oAttachment As Outlook.Attachment
Dim iAttachCnt As Integer
Dim i As Integer

' get count of attachments
MsgBox "Checking"
iAttachCnt = Item.Attachments.Count
If iAttachCnt > 0 Then
For i = 1 To iAttachCnt
Item.Attachments.Item(i).SaveAsFile _
Item.Attachments.Item(i).filename
'createFilename(Item.Attachments.Item(i).filename)
Next i
MsgBox "you have an attachment"
End If

End Sub
 
J

joannele71

Hi Sue,

Thank you very much for your explanation, suggestion and help. They are all
very helpful and the script works very well.

Joanne
 
M

Mies

Hello, i read all scipts and i am wondering if it is possible to make this
script for any folder in any pst file?
i have a lot of attachmments spread out over different archives, but i want
to get all the attachmenst out of my mail.
regards Michel
 
C

CBehan

Hi Joanne,

I've been looking for a way to do exactly the same thing. However, I'm not
a coder at all so I'm having some trouble with this. I managed to get the
script from your first posting to work only a couple of times. I've decided
to buy Sue's book to start learning but in the mean time could you please
post your final script. Thanks! Also, thanks to Sue for the great info!

Craig
 

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