Hi,
As I am new to the macros using VBA, having a query to share with and badly needed to survive..
Daily i would receive n number of mails to my inbox, based on the count i have to forward or redirect the mails to three recipients of my contacts.
Ex: if in inbox mail count is three (3), may be same subject line or different subject line. Those three mails should be send to three persons as "one email to A, second to B, third mail to C". This process should continue when there is three count in inbox. Please anyone can do the needful..
Thank you in advance.
Thanks & Regards
Chaitanya A
Thanks & Regards
Chaitanya A
I got so far in creating some code to perform the forwarding but then realised that you hadn't said what you wanted to happen to the original e-mails with forwarded copies.
I guess that the options are to move them to another folder or delete them or just mark them as read?
Martin
Eighty Twenty Spreadsheet Automation http://homepage.ntlworld.com/martin.rice1/ for all your Excel customisation and consulting needs.
If my solution has saved you time and/or money, please consider donating to Cancer Research UK.
Thanks & Regards
Chaitanya A
The following code appears to work for me when pasted into the ThisOutLookSession tab in the Outlook VBA editor.
It makes use of the Unread Mail search folder to determine how many unread mail items there are and sets the to read hence removing them from the Search Folder.Private Sub Application_NewMail() Dim MyInBox As Folder Dim oNS As NameSpace Dim myItem As MailItem Dim N As Integer Dim colStores As Outlook.Stores Dim oStore As Outlook.Store Dim oSearchFolders As Outlook.Folders Dim oFolder As Outlook.Folder Set colStores = Application.Session.Stores Set oStore = colStores(1) Set oSearchFolders = oStore.GetSearchFolders For Each oFolder In oSearchFolders If oFolder.Name = "Unread Mail" Then Exit For Next If oFolder.Items.Count >= 3 Then For N = 1 To 3 Set myItem = oFolder.Items(1).Forward Select Case N Case Is = 1 myItem.Recipients.Add "Person1@SomeISP.com" Case Is = 2 myItem.Recipients.Add "Person2@SomeISP.com" Case Is = 3 myItem.Recipients.Add "Person3@SomeISP.com" End Select oFolder.Items(1).UnRead = False myItem.Send DoEvents Next N End If End Sub
As set up, it does require that the Search folder is not picking up any other unread messages that you might not want to process.
You could probably adapt this to a different search folder with different criteria if required.
Last edited by mrice; 08-21-2011 at 03:02 PM.
Martin
Eighty Twenty Spreadsheet Automation http://homepage.ntlworld.com/martin.rice1/ for all your Excel customisation and consulting needs.
If my solution has saved you time and/or money, please consider donating to Cancer Research UK.
Hi Mrice,
Great!!!! It is working...!!
Thanks alot for the code...
A small help, if you wont' mind, how to mark the read mail with flag or to categorize..
Help me out...
Thanks & Regards
Chaitanya A
Try this
Private Sub Application_NewMail() Dim MyInBox As Folder Dim oNS As NameSpace Dim myItem As MailItem Dim N As Integer Dim colStores As Outlook.Stores Dim oStore As Outlook.Store Dim oSearchFolders As Outlook.Folders Dim oFolder As Outlook.Folder Set colStores = Application.Session.Stores Set oStore = colStores(1) Set oSearchFolders = oStore.GetSearchFolders For Each oFolder In oSearchFolders If oFolder.Name = "Unread Mail" Then Exit For Next If oFolder.Items.Count >= 3 Then For N = 1 To 3 Set myItem = oFolder.Items(1).Forward Select Case N Case Is = 1 myItem.Recipients.Add "Person1@SomeISP.com" Case Is = 2 myItem.Recipients.Add "Person2@SomeISP.com" Case Is = 3 myItem.Recipients.Add "Person3@SomeISP.com" End Select oFolder.Items(1).FlagIcon = 5 oFolder.Items(1).Save oFolder.Items(1).UnRead = False myItem.Send DoEvents Next N End If End Sub
Martin
Eighty Twenty Spreadsheet Automation http://homepage.ntlworld.com/martin.rice1/ for all your Excel customisation and consulting needs.
If my solution has saved you time and/or money, please consider donating to Cancer Research UK.
Hi Mrice,
If oFolder.Items.count >= 8 Then
at this line of code while executing im getting an error as..
Run-time error '91':
Object variable or With block variable not set
could you please help me out figuring the solution..
eagerly waiting for the reply..
Last edited by romperstomper; 08-24-2011 at 07:16 AM.
Thanks & Regards
Chaitanya A
Hi Mrice,
Hope, not irritating you..
When i tried the code in my regular outlook, it is worked, but when trying to execute in the outlook maintained by server is showing the error on and as..
If oFolder.Items.count >= 8 Then
at this line of code while executing im getting an error as..
Run-time error '91':
Object variable or With block variable not set
could you please help me out figuring the solution..
Is there any chance to write this code in a module and can execute than in this outlook session..
eagerly waiting for the reply..
Thanks & Regards
Chaitanya A
Is there an unread items search folder on the server version?
The error suggests that the code is not finding one.
Martin
Eighty Twenty Spreadsheet Automation http://homepage.ntlworld.com/martin.rice1/ for all your Excel customisation and consulting needs.
If my solution has saved you time and/or money, please consider donating to Cancer Research UK.
Unread search folder...??
The outlook is mapped in my machine as separate (Default) and the one has inbox where all the mails comes into..
Daily Hundreds of mails comes in..
can't we run the code or make the code in a module that which makes the same sense..?
Please help me..
Thanks & Regards
Chaitanya A
Hi mrice,
Private Sub Application_NewMail()
Dim MyInBox As Folder
Dim oNS As NameSpace
Dim myItem As Outlook.MailItem
Dim N As Integer
Dim colStores As Outlook.Stores
Dim oStore As Outlook.Store
Dim oSearchFolders As Outlook.Folders
Dim olFolder As Outlook.Folder
Dim i As Long
Dim ObjOutItem As Outlook.MailItem
Dim ObjOutName As Outlook.NameSpace
Dim ObjOutInbox As Outlook.MAPIFolder
Dim ObjOutApp As New Outlook.Application
'set the outlook objects
Set ObjOutApp = CreateObject("Outlook.Application")
Set ObjOutName = ObjOutApp.GetNamespace("MAPI")
Set ObjOutInbox = ObjOutName.GetDefaultFolder(olFolderInbox)
'If ObjOutInbox.Items.count = 0 Then
' MsgBox "Inbox is Empty", vbInformation, "Nothing Found"
' Exit Sub
'End If
'Set colStores = Application.Session.Stores
'Set oStore = colStores(1)
'Set oSearchFolders = oStore.GetSearchFolders
'For Each olFolder In oSearchFolders
' If olFolder.Name = "Unread Mail" Then Exit For
'Next
If ObjOutInbox.Items.count >= 8 Then
For N = 1 To 8
'Set myItem = olFolder.Items(1).Forward
Set ObjOutItem = ObjOutInbox.Items.Item(N).Forward
Select Case N
Case Is = 1
ObjOutItem.Recipients.Add "xyz@host.com"
Case Is = 2
ObjOutItem.Recipients.Add "abc@host.com"
Case Is = 3
ObjOutItem.Recipients.Add "por@host.com"
End Select
ObjOutInbox.Items(N).FlagIcon = 5
ObjOutInbox.Items(N).Save
ObjOutInbox.Items(N).UnRead = False
ObjOutItem.Send
DoEvents
Next N
End If
End Sub
I have tried modifying the code and able to run the above placed code. The thing is it is not getting looped. As three mails only getting forwarded and flagged. if the mails are above three and lets say upt 100, only three is forwarded and if i try to run once again the read and flagged mails are getting forwarded.
I knew that your help is putting me in to big deals.
help me out, please,..
Thanks & Regards
Chaitanya A
Hi
My original code made use of a search folder to identify unread e-mails. The alternative was to scan the inbox for unread e-mails which could take some time if your inbox contains many items (like mine).
I don't think that the current problems are caused by the location of the code. I need to repeat my question regarding whether the server version of Outlook has an Unread Mail Search folder - your local version obviously did. You will find it towards the bottom of the folder tree in the Mail Folders pane on the left hand side of the screen. If there is not one, you will need to create one on the server version with a right click.
Additionally, the code is designed to run in steady state starting with no unread mails and only doing anything substantial when the count gets to three. It does not deal well with a large backlog as it just processes three at a time when a new mail arrives (i.e. it only reduces the backlog by 3 - 1 = 2 mail items.)
I hope that this helps.
Martin
Eighty Twenty Spreadsheet Automation http://homepage.ntlworld.com/martin.rice1/ for all your Excel customisation and consulting needs.
If my solution has saved you time and/or money, please consider donating to Cancer Research UK.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks