syed_mushraf
Active Member
- Joined
- Oct 13, 2002
- Messages
- 265
I have different folders in the InBox as per the sender name. My requirment is to auto receive email in it by its respective name. Currently i need to forward it manually. plz help
'=============================================
'- SORT INBOX EMAILS INTO NAMED FOLDERS
'=============================================
Dim MyOlApp As Object
Dim MyNamespace As Object
Dim MyMailFolder As Object
Dim MyName As String
Dim MailBoxName As String
Dim MyMailItem As Object
Dim MailBody As String
Dim MailSenderName As String
Dim NameFolder As Object
Dim Counter As Integer
'-----------------------------------------------------------------------
Sub InboxTransfer()
Set MyOlApp = CreateObject("Outlook.Application")
Set MyNamespace = MyOlApp.GetNamespace("MAPI")
MyName = MyNamespace.CurrentUser
MailBoxName = "Mailbox - " & MyName
Set MyMailFolder = MyNamespace.Folders(MailBoxName).Folders("Inbox")
'-------------------------------------------------------------------
'- CHECK EMAILS IN INBOX
Counter = 0
For i = 1 To MyMailFolder.Items.Count
Set MyMailItem = MyMailFolder.Items(i)
MailSenderName = MyMailItem.SenderName
Application.StatusBar = " Checking mail for " & MailSenderName _
& " Found " & Counter & " so far.)"
'----------------------------------------------------------------
'- Try to set folder & trap error if it does not exist
On Error Resume Next
Set NameFolder = _
MyNamespace.Folders(MailBoxName).Folders(MailSenderName)
If Err.Number <> 0 Then
Err.Clear
rsp = MsgBox("Folder for " & MailSenderName _
& " does not exist.", vbCritical + vbOKCancel)
If rsp = vbCancel Then GoTo ClearObjects
Else
MyMailItem.Move NameFolder
Counter = Counter + 1
End If
Next
'-------------------------------------------------------------------
'- FINISH
MsgBox ("Transfer complete. " & Counter & " emails.")
Application.StatusBar = False
'-------------------------------------------------------------------
ClearObjects:
Set MyMailItem = Nothing
Set MyNamespace = Nothing
Set MyOlApp = Nothing
End Sub