PDA

View Full Version : Recipients or received account object in VBA



unicav
02-09-2011, 10:57 AM
Greetings all and let me say up front I'm an IT not a programmer, but I have to dabble in everything from bash to vbs with some projects. I normally can work something out with a little direction but so far I'm hitting a brick wall on this and I hope someone here can help. Please forgive me if I'm long winded here but I want to try and fully explain what's going on and what my goal is.

I am using a vba to save attachments from emails to specific folders. I have several email addresses that are used by sites to send us documents for archiving. The email addresses correspond to their areas and the type of document. The VBA has subroutines for each email account and I use a message rule for each account to fire the VBA and have the attachments saved to the correct directory. That's fine for the first 4-5 accounts that simply dump the PDF's into the proper directory. The remaining accounts require splitting the incoming PDF's into separate TIF pages for an automated system to analyze. I have a python script that handles that and it takes a couple arguments from the VBA, the Source File and the Destination Directory. What's bothering me is I'm winding up with a dozen message rules and a dozen subroutines in the VBA to handle everything. It's messy and redundant and I know there's a better way to handle it. But then again we're talking about VBA right?

What I want to do is find a way to look at the account that received the email, or look at the recipients list of the email, and use that to build an IF or CASE statement to determine the Destination Directory and if the python script needs to be called. Then I would only need one rule and one subroutine in the VBA. The problem is 99% of the discussions and examples I've found on the internet are geared toward sending mail, not receiving it, and the bits of code I've found relating to the recipients list are longer than my whole VBA with all it's subroutines.

So can anyone point out what I'm missing or is it simply going to be easier to just keep adding rules and subroutines to handle everything?

JP2112
02-10-2011, 01:56 PM
Outlook version?

You could write a single rule to pass all incoming mail through a script. Then use that script to parse the message and determine the appropriate action.

If you post your existing code (names changed to protect the innocent), someone here might be able to suggest an alternative.

unicav
02-10-2011, 02:24 PM
I'm using Outlook 2007.
I hate to post the code it's so tacky but yes you need something for reference. Can't fix it if you don't know what's broke. This is a simple edit of the Save Attachments With VBA code that's floating around the internet. I am using a rule for each email account to trigger a subroutine in the VBA that saves those attachments to the proper folder, and calls the processing script if needed. Here's the subroutine I'm using:

Sub SRRAP01(Item As Outlook.MailItem)
Dim olkFolder As Outlook.MAPIFolder, _
olkAttachment As Outlook.Attachment, _
objFSO As Object, _
sDestFolder As String, _
sFileName As String, _
sTargetName As String, _
sFileType As String

'File Save Location
sDestFolder = "O:\SRR\AP1\"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set olkFolder = Application.ActiveExplorer.CurrentFolder
If Item.Attachments.Count > 0 Then

For Each olkAttachment In Item.Attachments
Do While True
sFileType = objFSO.GetExtensionName(LCase(olkAttachment.FileName))
sFileName = objFSO.GetBaseName(olkAttachment.FileName) & Format(Item.CreationTime, "_yyyymmdd_hhnnss") & "." & sFileType
olkAttachment.SaveAsFile sWorkFolder & sFileName
Exit Do
Loop
Next
End If
Set oShell = CreateObject("WScript.Shell")
oShell.Run ("C:\Scripts\ProcessFiles.py " & sDestFolder)
Set oShell = Nothing
Set objFSO = Nothing
Set olkAttachment = Nothing
Set olkFolder = Nothing
End Sub

That's the one that calls the ProcessFiles.py script to split the PDF's into separate TIF files. The other accounts just save the item to the server.
But what I want to do is get the account or the TO SMTP address (srrap@mydomain.com for example) and use an IF block to find the right place for it. That way I'd only need one rule and one subroutine instead of having to keep adding subroutines for each email address.

Using Item.To I was able to make it work using this code:

Recip = LCase(Item.To)
If InStr(Recip, "srrap1") <> 0 Then sDestFolder = "\\Server\SRR\AP1\"
If InStr(Recip, "srrap2") <> 0 Then sDestFolder = "\\Server\SRR\AP2\"

But if someone adds the email address to their address book with a name like "Payables" then Item.To returns Payables, not srrap1@mydomain.com.
I installed Redemption today because a lot more objects are supposed to be accessible but I'm not using it properly because I haven't gotten a return on any item I've tried so far.

unicav
02-11-2011, 01:19 PM
Doesn't look like I'm going to get anywhere with this so I've opted to use the subject line instead. It adds the element of human error if someone doesn't put the right subject on the email but I've spent 2 weeks trying to find a way to use the recipients with no luck. time to move on.