Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7612255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:51:53+00:00 2026-05-31T01:51:53+00:00

I have a VBA Macro to search through email archives. When searching through tens

  • 0

I have a VBA Macro to search through email archives.

When searching through tens of thousands of emails, (or even just a couple hundred on my test machine) it displays the status for a few seconds, then enters a Not Responding state while running through the rest of the emails.

This has led impatient users to close out of the task prematurely, and I would like to rectify this by providing status updates.

I have coded the following solution, and believe that the problem lies in the way the GarbageCollector functions in VBA during the Loop.

Public Sub searchAndMove()

    UserForm1.Show

    ' Send a message to the user indicating
    ' the program has completed successfully, 
    ' and displaying the number of messages sent during the run.

End Sub

Private Sub UserForm_Activate()

Me.Width = 240
Me.Height = 60

Me.Label1.Width = 230
Me.Label1.Height = 50

Dim oSelectTarget As Outlook.Folder
Dim oMoveTarget As Outlook.Folder
Dim oSearchCriteria As String

' Select the target folder to search and then the folder to
' which the files should be moved
Set oSelectTarget = Application.Session.PickFolder
Set oMoveTarget = Application.Session.PickFolder

oSearchCriteria = InputBox("Input search string: ")

Dim selectedItems As Outlook.Items
Set selectedItems = oSelectTarget.Items
Dim selectedEmail As Outlook.MailItem

Dim StatusBarMsg As String
StatusBarMsg = ""

Dim initialCount As Long
initialCount = selectedItems.count


Dim movedCounter As Long
movedCounter = 0
Dim x As Long
Dim exists As Long

' Function Loop, stepping backwards
' to prevent errors derived from modifying the collection
For x = selectedItems.count To 1 Step -1
    Set selectedEmail = selectedItems.Item(x)
    ' Test to determine if the subject contains the search string

    exists = InStr(selectedEmail.Subject, oSearchCriteria)
    If Len(selectedEmail.Subject) > 999 Then
        selectedEmail.Move oMoveTarget
    Else:
        If exists <> 0 Then
            selectedEmail.Move oMoveTarget
            movedCounter = (movedCounter + 1)
        Else: End If
    End If
    Set selectedEmail = Nothing
    StatusBarMsg = "Processing " & x & " out of " & initialCount & " messages."

    UserForm1.Label1.Caption = StatusBarMsg
    UserForm1.Repaint
Next x

Dim Msg As String
Dim Response
Msg = "SearchAndMove has detected and moved " & movedCounter & _
  " messages since last run."
Response = MsgBox(Msg, vbOKOnly)


' Close the References to prevent a reference leak
Set oSelectTarget = Nothing
Set oMoveTarget = Nothing
Set selectedItems = Nothing
Set selectedEmail = Nothing

Unload Me

End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:51:54+00:00Added an answer on May 31, 2026 at 1:51 am

    Change the line

    UserForm1.Repaint

    to

    DoEvents

    Yes this will increase the execution time but in case there are thousands of emails then you don’t have much of an option.

    TIP:
    Also you might want to change

    StatusBarMsg = "Processing " & x & " out of " & initialCount & " messages."

    to

    StatusBarMsg = "Please do not interrupt. Processing " & x & " out of " & initialCount & " messages."

    Also it is advisable to inform your user at the beginning of the process that it might take time and hence they can run the process when they are sure they do not want to work on that pc?

    Something like this

    Sub Sample()
        Dim strWarning As String
        Dim Ret
    
        strWarning = "This process may take sometime. It is advisable to run this " & _
        "when you don't intend to use the pc for sometime. Would you like to Continue?"
    
        Ret = MsgBox(strWarning, vbYesNo, "Information")
    
        If Ret <> vbYes Then Exit Sub
    
        For x = SelectedItems.Count To 1 Step -1
    
        '~~> Rest of the code
    End Sub
    

    HTH

    Sid

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a VBA Word Macro that gets words from .txt list and color
I have written a VBA macro in an excel spreadsheet that opens all workbooks
I have CSV file and Macro in VBA. I want to open CSV file
I have a VBA macro for Microsoft Word that I am trying to improve.
I have a VBA publishing macro in Excel which generates a published workbook based
I have only basic VBA experince and my prior Macro experence was primarily with
I have a VBA macro that copies rows from one worksheet into another based
I have an Excel VBA macro that creates a folder tree based on an
I have just begun programming in VBA and despite my researching efforts have a
I have an Excel file that has a bunch of VBA and macro code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.