VBA for Outlook not quite working correctly to move emails–skipping some emails

I’ve written some poor VBA code (I am new to this) to move emails from one folder to another in Outlook based on a set of date ranges. It only partially works–it does the count correctly, but only moves a select few out of the full amount it should be moving. Any guidance is appreciated.


Sub MoveEmails()
    Dim SourceNamespace As Outlook.NameSpace
    Dim SourceFolder As Outlook.MAPIFolder
    Dim TargetNamespace As Outlook.NameSpace
    Dim TargetFolder As Outlook.MAPIFolder
    Dim StartDate As Date
    Dim EndDate As Date
    Dim EmailCount As Long
    Dim Response As VbMsgBoxResult
    
    ' Confirm with the user before proceeding
    Response = MsgBox("This might take a while. Would you like to proceed?", vbYesNo + vbQuestion, "Confirmation")
    If Response = vbNo Then Exit Sub
    
    ' Set reference to the source and target folders
    Set SourceNamespace = Application.GetNamespace("MAPI")
    Set SourceFolder = SourceNamespace.PickFolder ' Select the source folder
    Set TargetNamespace = Application.GetNamespace("MAPI")
    Set TargetFolder = TargetNamespace.PickFolder ' Select the target folder
    
    ' Show the source and target folders to the user
    MsgBox "Source Folder: " & SourceFolder.FolderPath & vbCrLf & "Target Folder: " & TargetFolder.FolderPath, vbInformation, "Folders Selected"
    
    ' Get the date range from the user
    StartDate = InputBox("Enter the start date (dd/mm/yyyy):")
    EndDate = InputBox("Enter the end date (dd/mm/yyyy):")
    
    ' Show the progress form
    frmProgress.Show vbModeless
    
    ' Call the recursive function to move emails and get the count of emails moved
    EmailCount = MoveFolderEmails(SourceFolder, TargetFolder, StartDate, EndDate)
    
    ' Hide the progress form
    Unload frmProgress
    
    ' Show a summary of the number of emails moved to the user
    MsgBox EmailCount & " emails have been moved.", vbInformation, "Operation Completed"
    
    ' Clean up
    Set SourceFolder = Nothing
    Set SourceNamespace = Nothing
    Set TargetFolder = Nothing
    Set TargetNamespace = Nothing
End Sub

and the function

Function MoveFolderEmails(SourceFolder As Outlook.MAPIFolder, TargetFolder As Outlook.MAPIFolder, StartDate As Date, EndDate As Date) As Long
    Dim SourceItems As Outlook.Items
    Dim Item As Object
    Dim Filter As String
    Dim Subfolder As Outlook.MAPIFolder
    Dim TargetSubfolder As Outlook.MAPIFolder
    Dim EmailCount As Long
    
    ' Set the date range for emails to be moved
    Filter = "[ReceivedTime] >= '" & Format(StartDate, "dd/mm/yyyy") & "' And [ReceivedTime] <= '" & Format(EndDate, "dd/mm/yyyy") & "'"
    
    ' Get the collection of items in the source folder that meet the criteria
    Set SourceItems = SourceFolder.Items.Restrict(Filter)
    
     ' Loop through each item in the collection and move it to the target folder
     For Each Item In SourceItems
        
        If TypeOf Item Is MailItem Then
            Item.Move TargetFolder
            
            ' Increment the count of emails moved and update the counter on screen
            
            EmailCount = EmailCount + 1
            frmProgress.lblCount.Caption = "Number of emails moved: " & EmailCount
            
            DoEvents ' Yield to other processes
            
        End If
        
        If TypeOf Item Is MailItem Then DoEvents ' Yield to other processes
       
          
    Next Item
    

    
    ' Loop through each subfolder in the source folder
    For Each Subfolder In SourceFolder.Folders
        On Error Resume Next
        Set TargetSubfolder = TargetFolder.Folders(Subfolder.Name)
        
        If Err.Number <> 0 Then
            Set TargetSubfolder = TargetFolder.Folders.Add(Subfolder.Name)
            Err.Clear
        End If
       
       EmailCount = EmailCount + MoveFolderEmails(Subfolder, TargetSubfolder, StartDate, EndDate)
       
       Set TargetSubfolder = Nothing
       
       On Error GoTo 0
       
    Next Subfolder
    
    MoveFolderEmails = EmailCount
    
End Function

I was expecting that it would move all the emails rather than what seems a random subset. I suspect that the amount needs to be chunked somehow for Outlook to be able to process? I am not sure!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật