Intermittent type mismatch error

L

lmcd

Hi, I have a macro which is running in Outlook 2003. Most of the time i
runs fine, however I am now getting intermittent type mismatch errors o
the line of code below

If itm.Class = olMail Then

itm is defined as an object. If I restart Outlook and run the macr
against the same emails it works fine. Any ideas would be appreciated
Thanks
 
T

The Code Cage Team

If its intermittent (but cant understand why) you will need to add som
error handling to your statement so that on error it will run the cod
again, if as you say it runs a second time fine

--
The Code Cage Tea

Regards,
The Code Cage Team
'The Code Cage' (http://www.thecodecage.com
 
L

lmcd

sorry, i maybe didnt explain too well, after it fails it will only ru
successfully after Outlook has been restarted. So far this is onl
happening on one pc, its running ok on two others
 
L

lmcd

This is the code before the error (minus Dim's)


Set nms = Application.GetNamespace("MAPI")

'Use specific folder
Set fld = nms.Folders.Item(arrGasFolders(0))

If Not fld Is Nothing Then
For I = 1 To UBound(arrGasFolders)
Set colFolders = fld.Folders
Set fld = Nothing
Set fld = colFolders.Item(arrGasFolders(I))
If fld Is Nothing Then
Exit For
End If
Next
End If

' Create excel object
Set appExcel = GetObject(, "Excel.Application")
appExcel.Workbooks.Open (strSheet)
Set wkb = appExcel.ActiveWorkbook
Set wks = wkb.Sheets(1)
wks.Activate
appExcel.Application.Visible = False


If fld Is Nothing Then
GoTo ErrorHandlerExit
End If

'Test whether selected folder contains mail messages
If fld.DefaultItemType <> olMailItem Then
MsgBox "Folder does not contain mail messages"
GoTo ErrorHandlerExit
End If

lngCount = fld.Items.Count

If lngCount = 0 Then
MsgBox "No messages to export"
GoTo ErrorHandlerExit
Else
Debug.Print lngCount & " messages to export"
End If



Set destFolder = nms.Folders.Item(arrArchiveFolders(0))

If Not destFolder Is Nothing Then
For I = 1 To UBound(arrArchiveFolders)
Set colFolders = destFolder.Folders
Set destFolder = Nothing
Set destFolder
colFolders.Item(arrArchiveFolders(I))
If destFolder Is Nothing Then
Exit For
End If
Next
End If


'Iterate through contact items in folder, and export a few fields
'from each item to a row in the worksheet

iNumContacts = fld.Items.Count
Set objItems = fld.Items

'Adjust i (row number) to be 1 less than the number of the first bod
row
I = 2


For iCount = 1 To iNumContacts

Set itm = fld.Items(iCount)


If itm.Class = olMail Then
'Process item only if it is a mail item

If (itm Is Nothing) Then

MsgBox "Error No: " & Err.Number & "; Description: "
Err.Description
GoTo ErrorHandlerExit

Else
Set msg = itm
End I
 
D

Dmitry Streblechenko

In this case dims are just as important as the actual code. How do you
declare the variables?

--
Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
-
 
L

lmcd

Declarations bloew. Thanks.

Dim appExcel As Excel.Application
Dim wkb As Excel.Workbook
Dim wks As Excel.Worksheet
Dim rng As Excel.Range
Dim strSheet As String
Dim I As Integer
Dim j As Integer
Dim lngCount As Long
Dim msg As Outlook.MailItem
Dim nms As Outlook.NameSpace
Dim fld As Outlook.MAPIFolder
Dim strFileName As String
'Must declare as Object because folders may contain different
'types of items
Dim itm As Object
Dim moveMail As Outlook.MailItem
Dim strTitle As String
Dim strPrompt As String
Dim destFolder As Outlook.MAPIFolder
Dim arrArchiveFolders() As String
Dim arrGasFolders() As String
Dim colFolders As Outlook.Folders
Dim iNumContacts As Integer
Dim iCount As Integer
Dim objItems As Outlook.Items
Dim NumItems As Integer
Dim dteMeter As Date
Dim strBackupFileName As String
Dim strFlagValue As String
Dim strDate As Strin
 
D

Dmitry Streblechenko

msg is declared as as Outlook.MailItem.
Are you sure the error is not coming from the

Set msg = itm

line?

--
Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
-
 
L

lmcd

Hi, thanks for the reply. I finally got to debug it today on the user
PC and it is the Set msg = itm line.

Why would this work fine on one machine and not another
 
D

Dmitry Streblechenko

If the folder only has MailItem objects, the code will work.
If you have other objects as well (reports, meeting requests, etc), it will
fail.

--
Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
-
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top