0

I am running a search in certain folders and their subfolders to move all items in a common parent folder.

When the search is done I get an Outlook.Results item rsts and then want to move all items in the search result. I am at the step where I loop all items, but the last item throws a 440 at the GetNext Method. How can I solve this?

---------------------------
Microsoft Visual Basic for Applications
---------------------------
Run-time error '440':

The object does not support this method.
---------------------------
OK   Help   
---------------------------

My Code

Public blnSearchComp As Boolean

Private Sub Application_AdvancedSearchComplete(ByVal SearchObject As Search)
    blnSearchComp = True
End Sub

Sub SearchAllEmailsInFolder(oFolder As Folder)
    Dim sch As Outlook.Search
    Dim rsts As Outlook.Results
    blnSearchComp = False
    
    Dim Scope As String
    Dim Filter As String
    Scope = "'" & oFolder.FolderPath & "'"
    Filter = "urn:schemas:mailheader:subject = '' OR urn:schemas:mailheader:subject <> ''"
    
    Set sch = Application.AdvancedSearch(Scope, Filter, True)
    While blnSearchComp = False
        DoEvents
    Wend
    Set rsts = sch.Results
    Debug.Print rsts.Count & vbCr & "------------"
    
    Dim i As Object
    Set i = rsts.GetFirst
    Do While Not i Is Nothing
        Debug.Print "Move '" & i.Subject & "' to '" & oFolder.Parent.FolderPath & "'"
        'i.Move(oFolder.Parent)
        i = rsts.GetNext ' <------------------- 440 thrown here, if it is last item.
    Loop
End Sub

Sub Start()
    Dim oRootFolder As Folder
    Set oRootFolder = ActiveExplorer.CurrentFolder
    
    LoopFolders oRootFolder
End Sub

Private Sub LoopFolders(oParentFolder As Folder)
    Dim f As Folder
    For Each f In oParentFolder.Folders
        If f.Name = "2774DD0F-B27D-41CA-9448-4BFBF1EC8AAB" Then
            'Search here
            Debug.Print f.FolderPath
            SearchAllEmailsInFolder f
        Else
            Debug.Print f.Name
            LoopFolders f
        End If
    Next
End Sub
Daniel
  • 3,092
  • 3
  • 32
  • 49
  • 1
    Use `for each i in rsts` instead of `do while` – Ike Nov 10 '21 at 07:04
  • Does this answer your question? [How to move each emails from inbox to a sub-folder](https://stackoverflow.com/questions/29779782/how-to-move-each-emails-from-inbox-to-a-sub-folder) – niton Jan 22 '22 at 22:00

0 Answers0