outlook VBA script run-time error 13 randomly while iterating emails in a public folder

痴心易碎 提交于 2019-12-25 04:42:09

问题


I am receiving a random run-time error 13 (type mismatch) executing the following subroutine. This routine works most of the time. The Folder passed in as an argument is legitimate at the time of the failure. From what I can see in the debugger, the objitem is missing some of the fields during runtime. After it break-points in the debugger, I can immediately single-step (re-executing the offending line) and there is no error.

I have attempted using 'on error goto' to sleep then retry various lines, and the error persists until it stops in the debugger.

I have also attempted changing between the For ii and For Each forms of the loop commands.

I have also temporarily disabled by anti-virus.

I am iterating over a large number of public folders. My outlook client is 2003 running under XP, and I am attached to Exchange server version 7654.

Can anyone tell me what I am failing to do (or if what I am attempting is not possible)?

Code below is modified per @dmitry suggestions and now works.

Sub SearchFolders(objFolder As Outlook.MAPIFolder)
    Dim objFolders As Outlook.Folders
    Dim subFolder As Outlook.MAPIFolder
    Dim Objitem As Outlook.MailItem
    Dim ii As Integer
    Dim ThisItem As Object
    Dim Items As Outlook.Items

' Recurse through all subfolders
    Set objFolders = objFolder.Folders
    For Each subFolder In objFolders
    Call SearchFolders(subFolder)
    Next subFolder

' Search the emails
    Set Items = objFolder.Items
    For ii = 1 To Items.Count
    Set ThisItem = Items.item(ii)
    If ThisItem.Class = olMail Then
        If VarType(ThisItem) = 9 Then GoTo NextdblLoop
        Set Objitem = ThisItem
        CheckEmailForErrorReports (objFolder.Items(ii))
        Set Objitem = Nothing
    End If
    Set ThisItem = Nothing
NextdblLoop:
    Next ii
    Set Items = Nothing
End Sub

回答1:


Firstly, do not use multiple dot notation; cache the Items collection before entering the loop.

Secondly, release the variables as soon as you are done with them

    dim item As Object
    dim Items as Outlook.Items
    set Items = objFolder.Items
    For ii = 1 To Items.Count
        set item = Items.Item(ii)
        If item.Class = olMail Then
            If TypeName(item) <> "MailItem" Then
                'THIS CAN NEVER HAPPEN. The check above is sufficient
                MsgBox ("Type mismatch: object s/b MailItem and is " & TypeName(item))
                GoTo NextdblLoop
            End If
            Set objitem = item 
            CheckEmailForErrorReports (objitem)
            Set objitem = Nothing
        End If
        Set item = Nothing
NextdblLoop:
    Next ii
End Sub


来源:https://stackoverflow.com/questions/28098914/outlook-vba-script-run-time-error-13-randomly-while-iterating-emails-in-a-public

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!