GirishDhruva
Active Member
- Joined
- Mar 26, 2019
- Messages
- 308
Hi Everyone,
Below i have a code to move all the mails to sub-folder but i am facing an error in highlighted line could any one suggest me why i am facing this error????
where users will provide their mail-id in cells A1 and A2 and folder name in cells B1 and B2
Regards
Dhruv
Below i have a code to move all the mails to sub-folder but i am facing an error in highlighted line could any one suggest me why i am facing this error????
Rich (BB code):
Option Explicit
Public Sub Move_Items()
Dim Inbox As Outlook.MAPIFolder
Dim SubFolder As Outlook.MAPIFolder
Dim olNs As Outlook.Namespace
Dim Item As Object
Dim Items As Outlook.Items
Dim lngCount As Long
Dim olApp As Variant
Dim Mail_Id1 As String
Dim Mail_Id2 As String
Dim Filename1 As String
Dim Filename2 As String
On Error GoTo MsgErr
Set olApp = CreateObject("Outlook.Application")
Set olNs = olApp.GetNamespace("MAPI")
Set Inbox = olNs.GetDefaultFolder(olFolderInbox)
Set Items = Inbox.Items
Mail_Id1 = Cells(2, "A").Value
Mail_Id2 = Cells(3, "A").Value
Filename1 = Cells(2, "C").Value
Filename2 = Cells(3, "C").Value
For lngCount = Items.Count To 1 Step -1
Set Item = Items(lngCount)
If Item.Class = olMail Then
Select Case Item.SenderEmailAddress
Case Mail_Id1
Set SubFolder = Inbox.Folders(Filename1)
Set Item = Items.Find("[SenderEmailAddress] =" & Mail_Id1)
If TypeName(Item) <> "Nothing" Then
Item.UnRead = True
Item.Move SubFolder
End If
Case Mail_Id2
Set SubFolder = Inbox.Folders(Filename2)
Set Item = Items.Find("[SenderEmailAddress] =" & Mail_Id2)
If TypeName(Item) <> "Nothing" Then
Item.UnRead = True
Item.Move SubFolder
End If
End Select
End If
Next lngCount
MsgErr_Exit:
Set Inbox = Nothing
Set SubFolder = Nothing
Set olNs = Nothing
Set Item = Nothing
Set Items = Nothing
Exit Sub
MsgErr:
MsgBox "An unexpected Error has occurred." _
& vbCrLf & "Error Number: " & Err.Number _
& vbCrLf & "Error Description: " & Err.Description _
, vbCritical, "Error!"
Resume MsgErr_Exit
End Sub
where users will provide their mail-id in cells A1 and A2 and folder name in cells B1 and B2
Regards
Dhruv