0

I could able to loop sender names from first columns of excel & nwow I am trying to create folder loop in below code but it showing error !!!

Sub MoveItems()
Dim myNameSpace As 
Outlook.Namespace

Dim myInbox As Outlook.Folder
Dim myDestFolder As Outlook.Folder
Dim myItems As Outlook.Items
Dim myItem As Object

Set myNameSpace =   Outlook.Application.GetNamespace("MAPI.   ")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderinbox)
Set myItems = myInbox.Items


Dim varSearchTerms As Variant
varSearchTerms = Range("a1:a3").Value 'refering to excel range for sender names

Dim varSearchTerm As Variant
For Each varSearchTerm In varSearchTerms

Set myItem = myItems.Find("[SenderName] = '" & varSearchTerm & "'")
 While TypeName(myItem) <> "Nothing"
Set myDestFolder = myInbox.Folders(range("b2"b3").value)
       myItem.Move myDestFolder
     Set myItem = myItems.FindNext
  Wend
   Next

 End Sub

Its showing error at this stage

  Set myItem = myItems.Find("[SenderName] = '" & varSearchTerm & "'")
     While TypeName(myItem) <> "Nothing"
    Set myDestFolder = myInbox.Folders(range("b2"b3").value)
           myItem.Move myDestFolder
         Set myItem = myItems.FindNext

Can anyone suggest how should i do it ?

  • If you're in Outlook, where the hell is that `Range` supposed to come from? You would need to start with and Excel Application object so you could access an open workbook to get to a sheet that contains a range. So in Outlook, you're at least 4 steps away from being able to say `Range` – HackSlash Sep 09 '22 at 18:12
  • 1
    You also have a bunch of typos. This code wouldn't compile. – HackSlash Sep 09 '22 at 18:13
  • `Range("a1:a3").Value` is going to return a string. You cannot `For...Next` a string. If we remove `.Value` then you get a range object. – HackSlash Sep 09 '22 at 18:13
  • Thanks for your reply, sorry for that i am still learning VBA, this is very simple.code but i am not able to.do it –  Sep 09 '22 at 19:37

0 Answers0