PDA

View Full Version : Solved: Type Mismatch error whilst scanning folder



DumboFixer
02-15-2006, 02:54 AM
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.

MOS MASTER
02-15-2006, 11:53 AM
Hi & Welcome to VBAX! :hi:

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:
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


HTH! :whistle:

matthewspatrick
02-15-2006, 07:07 PM
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 :banghead:

Patrick

DumboFixer
02-16-2006, 02:18 AM
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.

MOS MASTER
02-16-2006, 11:40 AM
Glad we could help! :thumb