PDA

View Full Version : Solved: Problems with automatic save attachment



ingridbl9
05-04-2006, 08:02 AM
Hi,
I am using Oulook 2003 and found and implemented the code by Killian to automatically save attachments in mails moved to a specific folder. This works fine as long as I manually move the mail in outlook. As soon as I install a rule to do this for me, the save of the attachment is no longer executed. Any idea what is going wrong ?
In the description it is mentioned this should work with a rule also...

This is the code I use :

'########################################################################## #####
'### Module level Declarations
'expose the items in the target folder to events
Option Explicit
Dim WithEvents TargetFolderItems As Items
'set the string constant for the path to save attachments
Const FILE_PATH As String = "C:\Temp\"

'########################################################################## #####
'### this is the Application_Startup event code in the ThisOutlookSession module
Private Sub Application_Startup()
'some startup code to set our "event-sensitive" items collection
Dim ns As Outlook.NameSpace
'
Set ns = Application.GetNamespace("MAPI")
Set TargetFolderItems = ns.Folders.Item( _
"Personal Folders").Folders.Item("ELSO").Items

End Sub

'########################################################################## #####
'### this is the ItemAdd event code
Sub TargetFolderItems_ItemAdd(ByVal Item As Object)
'when a new item is added to our "watched folder" we can process it
Dim olAtt As Attachment
Dim i As Integer

If Item.Attachments.Count > 0 Then
For i = 1 To Item.Attachments.Count
Set olAtt = Item.Attachments(i)
'save the attachment
olAtt.SaveAsFile FILE_PATH & olAtt.FileName

Next
End If

Set olAtt = Nothing

End Sub

'########################################################################## #####
'### this is the Application_Quit event code in the ThisOutlookSession module
Private Sub Application_Quit()

Dim ns As Outlook.NameSpace
Set TargetFolderItems = Nothing
Set ns = Nothing

End Sub

'########################################################################## #####
'### print routine
Sub PrintAtt(fFullPath As String)

Dim xlApp As Excel.Application
Dim wb As Excel.Workbook

'in the background, create an instance of xl then open, print, quit
Set xlApp = New Excel.Application
Set wb = xlApp.Workbooks.Open(fFullPath)
wb.PrintOut
xlApp.Quit

'tidy up
Set wb = Nothing
Set xlApp = Nothing

End Sub


I do not refer to the print routine for the moment.
The rule is quite simple and moves incoming mail from a certain address to the ELSO folder.

Another thing I tried out is to have this work under the Inbox instead of Personal Folders as I normally do not work with PF, but the Set TargetFolderItems does not seem to work on Inbox ? I receive run-time error '-1663827697(9cd7010f) : An object could not be found' when starting Outlook.


Thanks in advance,
Ingrid

Killian
05-08-2006, 01:31 AM
Hi Ingrid,

This should work fine with a rule, since it's using the Items_Add event - it shouldn't matter how the Item is added. Provided the Application_Startup code has run to set the folder items' event... :dunno

Regarding the Inbox: it is one of the fixed folders in Outlook (like the calendar, notes, task etc) so you need to use the GetDefaultFolder methodSet TargetFolderItems = ns.GetDefaultFolder(olFolderInbox).Items

ingridbl9
05-08-2006, 03:51 AM
Hi Killian,

Thx, the suggestion for the Inbox works perfect !
But with the rule it still isn't functioning :help
I am still a novice in Outlook VBA, so what do you mean when you say the Application_Startup Code must have run ?
Your help is much appreciated ...
Ingrid

ingridbl9
05-08-2006, 04:27 AM
Hi Killian,

Following your reply I did some debugging and the Application_Startup event code is processed allright, but the ItemAdd event is not triggered when I run my rule (which I already guessed). Are you sure this works at your end ? Is there any reason my Outlook is reacting differently ?

In addition, if I get the same document name, the Save As doesn't seem to overwrite the older version, which it should be doing for my purpose. Anything I can do about that ?

Thanks again - I am becoming a bit frustrated :(
Ingrid

Killian
05-08-2006, 08:47 AM
hmm... curious...
to be sure, I just tested it using the code posted in post#1:

Set the watched folder as "Temp" in "Personal Folders"
Set up a rule to move mail to Personal Folders>Temp
Ran the App-Startup routine
Sent some mail

Result: the rule moved the mail, the MailAdd event fired, attachments were saved.
I also checked what happens with a file of the same name - the existing (old) file is overwritten

So I'm a bit confused... I'll have a think about it.

ingridbl9
05-08-2006, 09:47 AM
puzzling indeed - I am correct in setting up the code under ThisOUtlookSession? or is this where I am going wrong ? :dunno

For the moment I am moving the mails manually as this triggers the event, but this is not as it should work as this doesn't give an immediate result on mail arrival.
Anything you can come up with I am willing to try out... learning all the time !

already big thanks
Ingrid

Killian
05-09-2006, 04:28 AM
puzzling indeed - I am correct in setting up the code under ThisOUtlookSession? or is this where I am going wrong ? :dunnothis is quite correct, so no problem there.

I have come across issues with the ItemAdd event failing to fire - these are usually related to processing large numbers of mailitems (MS KB article (http://support.microsoft.com/?kbid=290653)).
But I'm guessing you tested it with a single file to start with...

ingridbl9
05-09-2006, 05:31 AM
yes, of course - tried it out with anything from 1 up to 10 messages.
As I am probably stuck here, (have been looking around on microsoft sites but only reported problem is when too many items are moved), can't I work around this and forget about the rule altogether ?

Can't I add the check of the inbox in my vba code ? It only needs to check whether an incoming message has an attachment and the subject does not contain RE: or FW: (as in reply or forward) and then execute the save attachment. The mailbox concerned is only used for sending the documents that need to be saved.

Or am I asking too much now ? :think:

Appreciate the help - again -
Ingrid

Killian
05-09-2006, 08:57 AM
The fact that this code works fine for me but not for you (we're both on OL2003) makes me think there's something else going on here but while I was struggling to work out what that is, I found a much better way of doing the whole thing...

In the rules wizard, as well as moving your mail item, you can then choose to run a script. What I didn't realise, was that if you include a mailitem object as an argument to that routine, it will indeed refer to the mailitem the rule is applied to! Perfect!

So, set the rule for incoming mail to move the mail and run a script.
This is the script to run:Public Sub RuleScriptSaveAtt(Item As MailItem)

Dim olAtt As Attachment
Dim i As Integer

If Item.Attachments.Count > 0 Then
For i = 1 To Item.Attachments.Count
Set olAtt = Item.Attachments(i)
olAtt.SaveAsFile FILE_PATH & olAtt.FileName
Next
End If
Set olAtt = Nothing

End Sub

ingridbl9
05-09-2006, 01:07 PM
YESSS :cloud9:
Works perfect - I had seen the script option, but there again my knowledge ran short... I am just a mainframe programmer doing some Microsoft stuff on the side...

Anyway thanks very much for the prompt responses and the solution, I'll certainly keep my eye on your forum, maybe I can help someone else out sometime.

Ingrid

Killian
05-10-2006, 07:32 AM
Glad its working - we got there in the end :thumb

I've marked the thread as solved...

JAG836
01-15-2008, 08:28 AM
The fact that this code works fine for me but not for you (we're both on OL2003) makes me think there's something else going on here but while I was struggling to work out what that is, I found a much better way of doing the whole thing...

In the rules wizard, as well as moving your mail item, you can then choose to run a script. What I didn't realise, was that if you include a mailitem object as an argument to that routine, it will indeed refer to the mailitem the rule is applied to! Perfect!

So, set the rule for incoming mail to move the mail and run a script.
This is the script to run:Public Sub RuleScriptSaveAtt(Item As MailItem)

Dim olAtt As Attachment
Dim i As Integer

If Item.Attachments.Count > 0 Then
For i = 1 To Item.Attachments.Count
Set olAtt = Item.Attachments(i)
olAtt.SaveAsFile FILE_PATH & olAtt.FileName
Next
End If
Set olAtt = Nothing

End Sub

How would I use this to save the attachment to a specific folder on my network drive? The location I would like to save to is "L:\M5" and the file already exists so it needs to be overwritten every time the rule is run.

Also, is it ok if I chose to select more conditions in the set up rules wizard?

Thanks for the help as I am new to VBA!

JAG836
01-30-2008, 11:02 AM
anyone?

vish
09-17-2008, 10:55 PM
anyone?

Thanks for the update on the email attachment save in local disk's folder.

i have copied the given code in the ThisOutlookSession module.
how do i run it.
i have closed my Outlook and restarted it, but there i could not find any attachment from temp folder in outlook copied to c:\temp.

How do i do it. please guide.

Thanks
Vish:help

ingridbl9
09-18-2008, 11:48 AM
Thanks for the update on the email attachment save in local disk's folder.

i have copied the given code in the ThisOutlookSession module.
how do i run it.
i have closed my Outlook and restarted it, but there i could not find any attachment from temp folder in outlook copied to c:\temp.

How do i do it. please guide.

Thanks
Vish:help

ingridbl9
09-18-2008, 11:49 AM
Hi,

You have to run the script in a rule that is triggered on new arrival of mail or on a specific subject in new mail.

Good luck,
Ingrid

slhannah
03-24-2009, 03:12 PM
Trying to make the script below work for me, and it fails, with
Dim WithEvents TargetFolderItems aAs Items

not sure why??

rpz
05-20-2009, 07:28 AM
Can I set this up to watch two personal folders and save the attachments from each personal folder to different folders on my hard drive? I had the original code by Killian working great, but can't seem to get this to work on different folders.
Thanks in advance.

slhannah
05-20-2009, 08:01 AM
I haven't tried that... but I am having trouble with my Outlook runs not running automatically...?!?

rpz
05-21-2009, 09:52 AM
I played around and fixed my own question. I created and defined other arguments TargetFolderItems1 and FILE_PATH1, then duplicated the TargetFolderItems_ItemAdd routine and changed to changed it to save to FILE_PATH1. Everything seems to be working now.

Thanks to everyone for all of these posts. Now if someone could write some VBA to do the rest of my work for me too, I would appreciate it!

Lorentb
04-28-2010, 04:42 AM
Hello, How can I have this work eventhough the session is not up?

pendragon
07-15-2011, 08:26 AM
The fact that this code works fine for me but not for you (we're both on OL2003) makes me think there's something else going on here but while I was struggling to work out what that is, I found a much better way of doing the whole thing...

In the rules wizard, as well as moving your mail item, you can then choose to run a script. What I didn't realise, was that if you include a mailitem object as an argument to that routine, it will indeed refer to the mailitem the rule is applied to! Perfect!

So, set the rule for incoming mail to move the mail and run a script.
This is the script to run:Public Sub RuleScriptSaveAtt(Item As MailItem)

Dim olAtt As Attachment
Dim i As Integer

If Item.Attachments.Count > 0 Then
For i = 1 To Item.Attachments.Count
Set olAtt = Item.Attachments(i)
olAtt.SaveAsFile FILE_PATH & olAtt.FileName
Next
End If
Set olAtt = Nothing

End Sub

Hello

Would the above script have to be run in conjunction with the original script? If not and it can be used on its own to save attachments, as part of a Rule, wouldn't that mean an Outlook client does not need to be running? If so, where would the local C: drive be? On the Exchange server the mailbox resides? Or could attachments be saved to a UNC path?

Thanks in advance.