我有一个vba宏来搜索通过电子邮件归档。

当搜索成千上万的电子邮件时,(或者在我的测试机器上只有百百个),它会显示几秒钟的状态,然后在通过电子邮件的其余部分运行时进入没有响应状态。

这使得不耐烦的用户过早地关闭任务,我想通过提供状态更新来纠正这一点。

我已经编码了以下解决方案,并相信问题在于在循环期间的GBA中的GARBageCollector函数的方式。

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
.

有帮助吗?

解决方案

更改线路

UserForm1.Repaint

DoEvents

是这样,这将增加执行时间,但如果有数千个电子邮件,那么您没有大部分选项。

提示: 您也可能想要更改

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

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

还建议向您的用户通知您可能需要时间的过程中,因此当他们确定它们不想在该PC上工作时,他们可以运行该过程?

这样的东西

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top