VBA to process each email

Status
Not open for further replies.

txgator

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
First post here - was not able to find an answer using search - so here goes -

We have several large group email boxes at my company - and we are sent many emails.

Each customer of our company is assigned to a representative - at the company level. We have setup about 20 rules (not vba - rules) to process each email as it arrives. It is processed and moved to the representatives folder by the domain name. It seems to be working fine - however -

The list of customers is constantly changing and modifying the rules has become a nightmare - adding domains to this rep - deleting domains from this rep - no way to easily update the rules. The rules are also available to be modified by anyone that has access to the group box.

Is it possible to write VBA to process each email that arrives - and have it go through the same process?

If this is possible - I could take the assignment table in Excel - and write VBA code in Excel to build the case statements - and then just copy those statements and paste into the Outlook VBA -

I know - seems like a lot of work - but it will save me a LOT of time in the long run -

Any thoughts?

Thanks.
 

txgator

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Ok - found code that is working - but not sure it will work with large volumes -

Any suggestions? Thanks.

Code:
Private Sub Items_ItemAdd(ByVal item As Object)

  Dim posU As Integer
  Dim Msg As Outlook.MailItem
  Dim olApp As Outlook.Application
  Dim objNS As Outlook.NameSpace
  Set olApp = Outlook.Application
  Set objNS = olApp.GetNamespace("MAPI")
 
  ' default local Inbox
  Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
 
  On Error GoTo ErrorHandler
 
  Set objDestFolder = objNS.Folders("me@mycompany.com").Folders("ADS")
 
  If TypeName(item) = "MailItem" Then
    Set Msg = item
    ' ******************
        sEmail = item.SenderEmailAddress
        senderName = item.Sender

       
       

        posU = InStr(1, sEmail, "@yahoo", 1)
       
       
       
       
        If posU > 0 Then
            I = I + 1
            item.Move objDestFolder
        End If
       
       
       
    ' ******************
  End If
ProgramExit:
  Exit Sub
ErrorHandler:
  MsgBox Err.Number & " - " & Err.Description
  Resume ProgramExit
End Sub
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange

txgator

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Thanks Diane.

I did look at that thread as well - and like the thought of using the array to create the domain list and assign those to folders as needed.

I did not even think about using VBA to read in the table from Excel - and create the array as it was read -

If I could set the excel file on a file share - and have VBA read and create the array once a day - that would be even better -

Screen shot of the simple table that could be created in Excel and could be used to create the array is below.
 

Attachments

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
I have never done anything like that and I'm not sure where to start. Outlook would read the book on startup and load the array. I'm not sure if its workable in a rules macro. You'll either need to use a case statement or an array and I think an array is more efficient.
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Sorry, Diane, I'd already read the thread yesterday but was to tired to jump in :)

Reading the data of two columns is pretty fast, so you could even read it on every ItemAdd if that's really necessary. Something like that could be necessary if you want the users to get changes within the session else I'd do it on Outlook's startup as Diane suggested. If it's ok on startup, you could save all the needed target folder references globally, so you don't need to ref it in ItemAdd; that would make the code a little bit faster.

Here's an example of how to open the file: http://www.vboffice.net/smp=84
In your case you don't need to make the workbook visible. Once you've set the range you can use the Offset function to get the next column or row.

For large volumes I'd make the code as fast as possible. You don't need the olApp variable, directly use Application instead. You also don't need to call GetNamespace within Outlook. Do all the tests that could lead to leaving the function as early as possible. For instance, you don't need to ref the target folder, and then leave the function because it's not a mailitem.

This
Code:
If TypeOf item is MailItem
is faster than the string comparison. After checking the type it makes no sense to set a variable declared as MailItem on the item, and then not to use it. Using the Msg variable would be faster.

Also, think about whether you really want the MsgBox in case of an error poppping up. It could occur while nobody is on the desk, and it would stop Outlook from processing anythign else until someone clicks ok.
 

txgator

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Michael - Diane -

I really appreciate all of your feedback on this - it is going to save me so much time by having a workbook drive all of the rules - and eliminating all of the manual changes.

The user environment is setup like this - each user logs into their personal exchange account - and they also attach to one or more group boxes. The group box is where the rules currently exist - and I want to replace those rules with the VBA code I am going to design.

With that in mind - where is the Outlook VBA code stored? Will I have to give each user the code to read in the Excel data? Is there a way to have the code located in one place - and have the users access that code?

Also - within some of the existing rules - there are calls to REDIRECT emails - not forward them. I googled how to redirect using VBA and was surprised to find that VBA does not have a command to redirect - just forward. Any ideas on this one?

Look forward to completing this project - and be able to share the code with all of the users here.

Thanks.
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Outlook VBA is stored in Outlook, it cannot run on the server without Outlook. And you cannot easily deploy the VBA project, it must be installed manually for each user.

What is the difference between a redirect and a forward?
 

txgator

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
It will still be easier to manage - once I get the code written to open the excel file and populate the array.

The differences between automatically forwarding and redirecting an incoming message are as follows:
  • Message forwarding When a rule automatically forwards a message you receive, it leaves a copy of the message in your Inbox or in the folder to which the message was originally delivered. The rule then adds the designation "FW:" to the beginning of the Subject line, changes the message formatting, and then forwards the message to the account specified by the rule. The recipient represented by the account also sees that the message came from you.

  • Message redirection When a rule automatically redirects a message you receive, it also leaves a copy in your Inbox or in the folder to which the message was originally delivered. The rule then sends the message, unchanged, to the account specified by the rule. To the recipient, the message appears as though it came directly from the original sender. There is no indication that the message was delivered by way of your account.
I am wanting to do redirection on some of the emails that come in to the group box -
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
AFAIK you could simulate a redirection only if the sender is in your organization, and if you have the permission to send as that person. If it's an external sender, Outlook cannot redirect.
 
Status
Not open for further replies.
Top