Consulting

Results 1 to 5 of 5

Thread: Solved: Type Mismatch error whilst scanning folder

  1. #1

    Solved: Type Mismatch error whilst scanning folder

    Hi,

    This has probably been asked before - if it has then my apologies.

    We're trying to scan a folder in outlook and process each email in turn. That bit we can do but after a while we get a type mismatch error.

    This code snippet reporduced the problem.

    Dim oMail As Outlook.MailItem
    Dim counter1 As Integer

    Sub VL_PCP_error_report()

    counter1 = 0
    Set CurFolder = ActiveExplorer.CurrentFolder
    Set AllItems = CurFolder.Items
    Debug.Print ActiveExplorer.CurrentFolder

    For Each oMail In AllItems
    counter1 = counter1 + 1
    Debug.Print counter1
    Next
    End Sub

    It will loop a number of times then generate the mismatch error.

    Any ideas whats going wrong (and how we get round it) ?

    There are over 1000 emails in the folder but it's going wrong at number 965 (not that I think that's relevant). Each email is around 20K.

    I've also seen it in folders containing a few hundred emails.

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi & Welcome to VBAX!

    I'm not able to reproduce this error with a folder containing over 5000 emails. (outlook 2003)

    The TypeMismatch IMO could be (not sure though) because there's another type of Item between you're emails.

    Of course you can easily bypass your error with some easy errorhandling code in your sub. But I think its worth a try to make sure the Email is of Class olMail (Mailitem nr 43) before you do something else.

    Something like this would do the trick:[vba]
    Option Explicit
    Sub VL_PCP_error_report()
    Dim CurFolder As Outlook.MAPIFolder
    Dim AllItems As Outlook.Items
    Dim oMail As Object
    Dim counter1 As Integer
    counter1 = 0
    Set CurFolder = ActiveExplorer.CurrentFolder
    Set AllItems = CurFolder.Items

    Debug.Print ActiveExplorer.CurrentFolder
    For Each oMail In AllItems
    If (oMail.Class = olMail) Then
    counter1 = counter1 + 1
    Debug.Print counter1
    Else
    MsgBox "No mailitem here but.. Classnumber is: " & oMail.Class
    End If
    Next

    Set AllItems = Nothing
    Set CurFolder = Nothing
    End Sub
    [/vba]

    HTH!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  3. #3
    VBAX Expert
    Joined
    Jul 2004
    Location
    Wilmington, DE
    Posts
    600
    Location
    Your problem is almost certainly due to an unexpected type.

    For example, a while back I tried to write a sub that would cycle through everything in my Calendar. It kept bombing midway through the process, and I could not figure out why.

    Eventually I determined that it was bombing on an "appointment" that had begun its life as a regular message, and that I dragged into the Calendar folder to create an appointment. Outlook still treated it as a MailItem, apparently

    Patrick

  4. #4
    Hi Guys,

    thanks for the replies. The emails are all the same format (automatically generated by a tool) and are filed by a rule. There are no unexpected emails in the folder.

    I've added some basic error handling just to log the "faulty" messages for manual handling.

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Glad we could help!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •