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.
 
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
 
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

  • CustomerAssignmentList.jpg
    CustomerAssignmentList.jpg
    42.8 KB · Views: 638
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.
 
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.
 
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.
 
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?
 
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 -
 
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.
Similar threads
Thread starter Title Forum Replies Date
S Outlook VBA rule script to process both MailItem and MeetingItem Using Outlook 0
efire9207 VBA Outlook Contacts Outlook VBA and Custom Forms 6
B Requesting VBA code to make Outlook prompt for confirmation when deleting a task? Outlook VBA and Custom Forms 4
M Outlook 365 VBA Auto-Forward Only the first of Duplicate Emails Outlook VBA and Custom Forms 2
N VBA Code Not Working correctly Outlook VBA and Custom Forms 1
L VBA to Triage Incoming Email Outlook VBA and Custom Forms 0
J Outlook VBA to send from Non-default Account & Data Files Outlook VBA and Custom Forms 4
H using VBA to edit subject line Outlook VBA and Custom Forms 0
G Get current open draft message body from VBA Outlook VBA and Custom Forms 1
P VBA to add email address to Outlook 365 rule Outlook VBA and Custom Forms 0
M Outlook 2016 outlook vba to look into shared mailbox Outlook VBA and Custom Forms 0
V VBA Categories unrelated to visible calendar and Visual appointment Categories Outlook VBA and Custom Forms 2
D Outlook VBA forward the selected email to the original sender’s email ID (including the email used in TO, CC Field) from the email chain Outlook VBA and Custom Forms 2
R Outlook 365 VBA AUTO SEND WITH DELAY FOR EACH EMAIL Outlook VBA and Custom Forms 0
R Outlook 2019 VBA to List Meetings in Rooms Outlook VBA and Custom Forms 0
geoffnoakes Counting and/or listing fired reminders via VBA Using Outlook 1
O VBA - Regex - remove double line spacing Outlook VBA and Custom Forms 1
D.Moore Strange VBA error Outlook VBA and Custom Forms 4
B Modify VBA to create a RULE to block multiple messages Outlook VBA and Custom Forms 0
D Outlook 2021 Using vba code to delete all my spamfolders not only the default one. Outlook VBA and Custom Forms 0
K vba code to auto download email into a specific folder in local hard disk as and when any new email arrives in Inbox/subfolder Outlook VBA and Custom Forms 0
D VBA - unable to set rule condition 'on this computer only' Outlook VBA and Custom Forms 5
L Fetch, edit and forward an email with VBA outlook Outlook VBA and Custom Forms 2
BartH VBA no longer working in Outlook Outlook VBA and Custom Forms 1
W Can vba(for outlook) do these 2 things or not? Outlook VBA and Custom Forms 2
MattC Changing the font of an email with VBA Outlook VBA and Custom Forms 1
P MailItem.To Property with VBA not work Outlook VBA and Custom Forms 2
P Tweak vba so it can target another mailbox Outlook VBA and Custom Forms 1
A Outlook 2010 VBA fails to launch Outlook VBA and Custom Forms 2
richardwing Outlook 365 VBA to access "Other Actions" menu for incoming emails in outlook Outlook VBA and Custom Forms 0
W Create a Quick Step or VBA to SAVE AS PDF in G:|Data|Client File Outlook VBA and Custom Forms 1
J Outlook Rules VBA Run a Script - Multiple Rules Outlook VBA and Custom Forms 0
C Outlook (desktop app for Microsoft365) restarts every time I save my VBA? Using Outlook 1
D VBA Macro to Print and Save email to network location Outlook VBA and Custom Forms 1
TedSch Small vba to kill political email Outlook VBA and Custom Forms 3
E Outlook 365 Outlook/VBA Outlook VBA and Custom Forms 11
N VBA Macro To Save Emails Outlook VBA and Custom Forms 1
Z VBA Forward vs manual forward Outlook VBA and Custom Forms 2
J VBA Cannot programmatically input or change Value for User Defined field Using Outlook 1
J VBA for outlook to compare and sync between calendar Outlook VBA and Custom Forms 1
A Any way to force sort by/group by on search results with VBA? Outlook VBA and Custom Forms 1
E Default shape via VBA Outlook VBA and Custom Forms 4
A Change settings Send/receive VBA Outlook VBA and Custom Forms 0
Z Import Tasks from Access Using VBA including User Defined Fields Outlook VBA and Custom Forms 0
E Outlook VBA change GetDefaultFolder dynamically Outlook VBA and Custom Forms 6
justicefriends How to set a flag to follow up using VBA - for addressee in TO field Outlook VBA and Custom Forms 11
M add new attendee to existing meetings with VBA Outlook VBA and Custom Forms 5
D VBA code to select a signature from the signatures list Outlook VBA and Custom Forms 3
D Create advanced search (email) via VBA with LONG QUERY (>1024 char) Outlook VBA and Custom Forms 2
David McKay VBA to manually forward using odd options Outlook VBA and Custom Forms 1

Similar threads

Back
Top