View Full Version : Solved: Autosave file with SUBJECT as filename
110kiley
08-10-2012, 08:08 AM
First, thanks in advance. Either for the code or the links for help...
Using Outlook 2010 on Exchange. I'll have scans emailed with PDF attachments. Subjects will be unique to email. I'd like to include a script in the rule to save the file using the SUBJECT of the email. After reading many sites and fiddling with code (obvious noob here), it's simply not working. See below for where I am. Laughter allowed. :)
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim strFile As String
Dim strFolderpath As String
Dim strFileName As String
Dim objSubject As String
strFolderpath = "C:\Users\kmurphy\Documents\Dropbox\Public File"
objSubject = objMsg.subject
strFileName = objSubject & ".pdf"
strFile = strFolderpath & strFileName
For Each objAtt In itm.Attachments
objAtt.SaveAsFile strFile
Set objAtt = Nothing
Next
End Sub
BrianMH
08-10-2012, 09:04 AM
Try adding changing your path to "C:\Users\kmurphy\Documents\Dropbox\Public File\"
I'm guessing if you looked in C:\Users\kmurphy\Documents\Dropbox there are a bunch of files with Public File appended to the name.
Also keep in mind if there are multiple attachments it will overwrite all but the last.
110kiley
08-10-2012, 09:28 AM
I was hopeful.... but no files exist in the parent either. Searched entire computer (including attached servers) for PDFs created in the past two days, and there are none. Seems that it simply isn't 'firing'? No error messages though...
BrianMH
08-10-2012, 09:38 AM
How are you firing it?
Also set option explicit to on. You have used an undeclared variable.
objSubject = objMsg.Subject
Needs to be
objSubject = itm.Subject
BrianMH
08-10-2012, 09:41 AM
Just tested this with an outlook rule on my system and it worked as expected (with the correct variable).
110kiley
08-10-2012, 09:57 AM
EUREKA! LOVE it when the answer is simple! Thank you so much BrianMH!
And at the risk of looking a gift-horse in the mouth.... Any thoughts on this:
Expanding on the same issue above, have the code search the subject for text and then match that with a folder containing same, to save to that folder?
For example... subject has "Black123456invoice-1" and corresponding sub-folder for attachments on HDD contains the word "Black". So code would 'see' "black" within subject and save to the folder containing the word "Black".
If I should post this as new query to forum, please let me know.
Either way, very grateful for your help! :)
BrianMH
08-10-2012, 11:14 AM
NP. Make sure to update your system to always use option explicit. That makes things much easier to debug. This should do what you want. Note I changed objSubject to strSubject as it makes it easier to see the data type.
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim strFile As String
Dim strFolderpath As String
Dim strFileName As String
Dim strSubject As String
strSubject = objMsg.subject
if ucase(strSubject) like "*BLACK*" then
strFolderPath = "C:\Black\"
elseif ucase(strSubject) like "*BLUE*" then
strFolderPath = "C:\Blue\"
elseif ucase(strSubject) like "*RED*" then
strFolderPath = "C:\Red\"
else strFolderPath = "C:\Default\"
end if
strFileName = strSubject & ".pdf"
strFile = strFolderpath & strFileName
For Each objAtt In itm.Attachments
objAtt.SaveAsFile strFile
Set objAtt = Nothing
Next
End Sub
110kiley
08-10-2012, 12:06 PM
FANTASTIC! Tested and works like a charm. Just one question... "UCase" seems to be having some affect. Is there a way to make this not case-sensitive? All attachments went to default folder except those whose folder name was all caps.
Again, thank you for your time.
110kiley
08-10-2012, 12:07 PM
Oh, and for reference, here's my code. Note that CFEIG is the only one that went to the appropriate folder.
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim strFile As String
Dim strFolderpath As String
Dim strFileName As String
Dim strSubject As String
strSubject = itm.subject
If UCase(strSubject) Like "*Black*" Then
strFolderpath = "C:\Users\kmurphy\Documents\Dropbox\Public File\Black\"
ElseIf UCase(strSubject) Like "*Desjarlais*" Then
strFolderpath = "C:\Users\kmurphy\Documents\Dropbox\Public File\Desjarlais\"
ElseIf UCase(strSubject) Like "*Maggart*" Then
strFolderpath = "C:\Users\kmurphy\Documents\Dropbox\Public File\Maggart\"
ElseIf UCase(strSubject) Like "*Corker*" Then
strFolderpath = "C:\Users\kmurphy\Documents\Dropbox\Public File\Corker\"
ElseIf UCase(strSubject) Like "*Congelecpac*" Then
strFolderpath = "C:\Users\kmurphy\Documents\Dropbox\Public File\Congelecpac\"
ElseIf UCase(strSubject) Like "*CFEIG*" Then
strFolderpath = "C:\Users\kmurphy\Documents\Dropbox\Public File\CFEIG\"
ElseIf UCase(strSubject) Like "*Zelenik*" Then
strFolderpath = "C:\Users\kmurphy\Documents\Dropbox\Public File\Zelenik\"
ElseIf UCase(strSubject) Like "*Sargent*" Then
strFolderpath = "C:\Users\kmurphy\Documents\Dropbox\Public File\Sargent\"
ElseIf UCase(strSubject) Like "*Williamson*" Then
strFolderpath = "C:\Users\kmurphy\Documents\Dropbox\Public File\Williamson\"
Else: strFolderpath = "C:\Users\kmurphy\Desktop\RandomPoliticalScans\"
End If
strFileName = strSubject & ".pdf"
strFile = strFolderpath & strFileName
For Each objAtt In itm.Attachments
objAtt.SaveAsFile strFile
Set objAtt = Nothing
Next
End Sub
BrianMH
08-10-2012, 12:11 PM
The ucase is there to make it not case sensitive. Make sure to put your criteria in all caps. Otherwise if files come in with different cases they won't meet the criteria.
so
x = a
or
x = A
if ucase(x) = "A" 'this statement is true either way above.
if x = "A" 'this statement is only true in once of those cases.
if x = "a" 'this statement is only true in once of those cases also.
it is a good habit when dealing with strings that may have varying cases to test the string with the ucase() function and type your criteria as caps (or you could ucase() your criteria too.
So change your criteria ie "*Desjarlais*" should be "*DESJARLAIS*"
110kiley
08-10-2012, 12:15 PM
Very instructive, and it is a thing of beauty now. Thank you!
Cheers!
BrianMH
08-10-2012, 12:30 PM
Glad I could help!
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.