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

  • CustomerAssignmentList.jpg
    CustomerAssignmentList.jpg
    42.8 KB · Views: 538

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.
Similar threads
Thread starter Title Forum Replies Date
S Outlook VBA rule script to process both MailItem and MeetingItem Using Outlook 0
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 0
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
FryW Need help modifying a VBA script for in coming emails to auto set custom reminder time Outlook VBA and Custom Forms 0
S vba outlook search string with special characters Outlook VBA and Custom Forms 1
S VBA search string with special characters Outlook VBA and Custom Forms 1
U Outlook 2019 VBA run-time error 424 Outlook VBA and Custom Forms 2
DDB VBA to Auto Insert Date and Time in the signature Outlook VBA and Custom Forms 2
F VBA to move email from Non Default folder to Sub folders as per details given in excel file Outlook VBA and Custom Forms 11
G VBA to save selected Outlook msg with new name in selected network Windows folder Outlook VBA and Custom Forms 1
F Excel VBA to move mails for outlook 365 on secondary mail account Outlook VBA and Custom Forms 1
B Zoom automatically next email item (VBA) Outlook VBA and Custom Forms 2
T vba extract data from msg file as attachment file of mail message Outlook VBA and Custom Forms 1
K Outlook Office 365 VBA download attachment Outlook VBA and Custom Forms 2
A VBA Script - Print Date between first email in Category X and last email in Category Y Outlook VBA and Custom Forms 3
N Help creating a VBA macro with conditional formatting to change the font color of all external emails to red Outlook VBA and Custom Forms 5
N Save selected messages VBA does not save replies and/or messages that contain : in subject Outlook VBA and Custom Forms 1
Y Filter unread emails in a search folder vba help Outlook VBA and Custom Forms 0
V vBA for searching a cell's contents in Outlook and retrieving the subject line Outlook VBA and Custom Forms 1
B vBA for exporting excel file from outlook 2016 Outlook VBA and Custom Forms 3
L Modifying VBA script to delay running macro Outlook VBA and Custom Forms 3
L Need help modifying a VBA script for emails stuck in Outbox Outlook VBA and Custom Forms 6
K can't get custom form to update multiple contacts using VBA Outlook VBA and Custom Forms 3
S Excel vba code to manage outlook web app Using Outlook 10
H Custom Outlook Contact Form VBA Outlook VBA and Custom Forms 1
S Problem Checking the available stores in my Inbox (Outlook VBA) Outlook VBA and Custom Forms 0
S Outlook VBA How to adapt this code for using in a different Mail Inbox Outlook VBA and Custom Forms 0
S Add VBA save code Using Outlook 0
C Auto Run VBA Code on new email Outlook VBA and Custom Forms 1
O VBA Cases with Listbox - Can you use Multi-Select? Outlook VBA and Custom Forms 4

Similar threads

Top