-
Feb 27th, 2015, 03:28 PM
#1
Thread Starter
New Member
[RESOLVED] Save Email Attachment from 2 Emails with Different Rules
Hi,
I have the following code to save an email attachment to a network drive based on a specific subject line:
Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
saveFolder = "T:\MyDirecotry\MySubDirectory"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & "DataFile1"
Next
End Sub
The code is okay but I cannot adapt it to fit my purpose. I have 2 emails that come to me each workday and each email contains one Excel file. Each email contains unique text in the subject line. My goal is to save the files to the same directory and so my directory would have DataFile1.xls and DataFile2.xls. I want each file to be overwritten with the same exact name. I tried creating Module2 in Outlook but that did not work----I did not get a secondary script to choose from in the Outlook mail rules. I also need to make sure that the files are from the most current email in my Inbox in Outlook, since I only do an email cleanup every few months or so.
Any help would be appreciated!
Thanks,
Phillycheese
-
Feb 27th, 2015, 04:02 PM
#2
Re: Save Email Attachment from 2 Emails with Different Rules
you could use separate rules based on the subject, each rule can use different procedure
else you can use the new mail event to process emails, just when they arrive,
note also that datafile1 should have the .xls extention
you can just try changing like
Code:
objAtt.SaveAsFile saveFolder & "\" & objatt.filename
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Feb 27th, 2015, 04:10 PM
#3
Thread Starter
New Member
Re: Save Email Attachment from 2 Emails with Different Rules
Hi WC1, thanks for the reply. Right now when I create the rule in Outlook and it asks for a "script" and I click on it to make a selection, I only get "Project1.saveAttachtoDisk". Can you tell me how to create another procedure? I tried copying the code from Module1 and creating Module2 with adjustments for the different subject line, but no luck. Also, any suggestions for using the most recent emails in the Inbox?
-
Feb 28th, 2015, 03:27 AM
#4
Re: Save Email Attachment from 2 Emails with Different Rules
Can you tell me how to create another procedure?
just make another Sub in the same module, copy the code and edit as required, make sure to have different name for 2nd procedure
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Mar 2nd, 2015, 01:10 PM
#5
Thread Starter
New Member
Re: Save Email Attachment from 2 Emails with Different Rules
Hi, I tried to change my code to the following, and added a corresponding second rule in Outlook:
'Daily Email FIRST
Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
saveFolder = "T:\MyDirecotry\MySubDirectory"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & "DataFile1.xls"
Next
End Sub
'Daily Email SECOND
Sub saveAttachtoDisk2(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
saveFolder = "T:\MyDirecotry\MySubDirectory"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & "DataFile2.xls"
Next
End Sub
I deleted all but the most current email I have for each of the rules from my inbox to run a test. When I run the rules nothing happens. I ran the test on both read and un-read emails in my inbox. Then I tried deleting the second rule to see if I could get back to having at least the first one be okay, but that didn't work either. Do you spot anything bad in the code I have?
-
Mar 2nd, 2015, 03:27 PM
#6
Re: Save Email Attachment from 2 Emails with Different Rules
When I run the rules nothing happens.
how do you know? does the procedure run but not save attachment or not run at all?
what rule have you created? check messages when they arrive or what, i doubt this will run with already arrived messages
you really only need one rule and procedure as both do exactly the same apart from the file name, which can be determined within the procedure
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Mar 2nd, 2015, 04:18 PM
#7
Thread Starter
New Member
Re: Save Email Attachment from 2 Emails with Different Rules
The procedure runs...I go to "Manage Rules & Alerts" and then click "Run Rules Now" and I run both rules on all messages which includes both read and un-read email. I also re-sent myself the emails so they are new and un-read but the rule did not work. I can verify this by looking in the directory where the files are supposed to show up. The rule itself is the following:
Apply this rule after the message arrives
with "Pricing Sheet" in the subject
and which has an attachment
and on this computer only
run "Project1.saveAttachtoDisk"
My other rule is similar but substitutes "Pricing Data" for the subject, and runs "Project1.saveAttachtoDisk2".
Any ideas on how to fix?
-
Mar 3rd, 2015, 04:15 AM
#8
Re: Save Email Attachment from 2 Emails with Different Rules
put a break point at the first line of the procedure, the code will stop running at the break point, then you can step through the code to see what is happening
if you are not sure about putting a break point, click in the left margin of the code module, you should get a red blob in the margin and the line of code back coloured in red
F5 to step through code line by line
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Mar 3rd, 2015, 08:43 AM
#9
Thread Starter
New Member
Re: Save Email Attachment from 2 Emails with Different Rules
Okay, I did that and when I use F5 to go through the code, a "Macros" dialog box pops up with no macro names listed. The drop-down box at the bottom has 2 options: "<All Projects>" or "Project1 (VbaProject.OTM)". But neither will allow me to click on them and the only button in the pop-up box I can hit is "Cancel".
Is there some other declaration I'm supposed to have at the top? I am totally lost here.
-
Mar 3rd, 2015, 03:05 PM
#10
Re: Save Email Attachment from 2 Emails with Different Rules
as your procedure have parameter, they can not be run from macros and would need to be started by their event, either from the rules dialog or by receiving appropriate new mail
in either case the code will break at the break point, then F5 to continue each step
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Mar 3rd, 2015, 04:49 PM
#11
Thread Starter
New Member
Re: Save Email Attachment from 2 Emails with Different Rules
I tried using the F5 but it just keeps prompting me for the macro. I tried to use some options from the menu bar like Debug > Run to Cursor and a bunch of other random things but I can't get this to work. I've spent hours trying this with old and new emails, re-running the rules and really just taking shots in the dark. I was hoping there was a solution but I guess I'll keep downloading these manually everyday. I appreciate the help but I give up!
-
Mar 4th, 2015, 04:24 AM
#12
Re: Save Email Attachment from 2 Emails with Different Rules
I was hoping there was a solution
of course there is, but unlike excel or word where you can post a test workbook or document, in outlook you can not do that, so we can only help you to find the problem
have you put a breakpoint in the procedure?
anytime the procedure runs it should stop at the breakpoint
you can only force the procedure to run from the rules dialog, or by having an appropriate new message arrive
first you need to recheck that your messages still match your rule criteria
if you have any doubt that a second rule may be causing a problem, remove the second rue, until the first is working again
save all your code and restart outlook
if the code does not stop at the breakpoint, you must assume that the procedure is not running at all, check your rules again
try changing the criteria for your rules to find why the rule is not firing the code
you could try using a rule for all new messages, then allow the code to process the criteria like
Code:
Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
if instr(1, item.subject, "Pricing", vbtextcompare) > 0 then
saveFolder = "T:\MyDirecotry\MySubDirectory"
if instr(item.subject, "Pricing Sheet") > 0 then
myfile = "Datafile1.xls"
elseif instr(tem.subject, "Pricing Data") > 0 then
myfile = Datafile2.xls
else
msgbox "Message subject contained 'Pricing', but did not match criteria" & vbnewline & itmem.subject & vbnewline & "Nothing saved"
exit sub
end if
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & myfile
Next
end if
End Sub
i have not tested this code, but all emails not containing pricing the subject should be ignored, depending on whether pricing sheet or pricing data as to the file name
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Mar 4th, 2015, 04:57 PM
#13
Thread Starter
New Member
Re: Save Email Attachment from 2 Emails with Different Rules
Thank you, I will try to get this going using the new code you provided. I will have more time tomorrow and I will let you know how it goes. I appreciate your patience as I would love to find a solution!
-
Mar 5th, 2015, 03:32 PM
#14
Thread Starter
New Member
Re: Save Email Attachment from 2 Emails with Different Rules
I am still unable to get this to work. I spoke to a colleague who is going to do some testing with it and I will leave this as unsolved for now if that is okay. I will report back in the next few days with an update---if he can get it to work it may be of benefit to share the issue with others.
-
Mar 27th, 2015, 08:27 AM
#15
Thread Starter
New Member
Re: Save Email Attachment from 2 Emails with Different Rules
Hi,
Sorry for the long delay. My colleague did some testing and it appears as if there were some security settings that caused problems with the macro. It had to do with some kind of pop-up that asked about information being digitally signed but I could not see that on my computer (so I did not know anything needed to be disabled or skipped over). The adjustment was to lower the security settings and that seemed to get the macro to work okay. I know that is not very specific in terms of the exact language/syntax of the error, but hopefully it will aid others as to what may be a problem if they run into issues.
Thanks for the help on this,
Phillycheese
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|