Runtime error 13: Type mismatch for loops

nuckfuts

New Member
Joined
Mar 10, 2020
Messages
47
Office Version
  1. 365
Platform
  1. Windows
I've been using some code that involves loops and it has been working fine every time but without changing the code I now run into a type mismatch error. Nothing in the code was change and my watches window shows the following:

ExpressionValueTypeContext
k11IntegerSheet1.SendEmail
olItems(k)Variant/Object/ReportItemSheet1.SendEmail
olMail[subject of email found]MailItem/MailItemSheet1.SendEmail

I've condensed my code below: I'm really not sure why it worked 20+ times but now gives an error?

VBA Code:
Dim olApp As Outlook.Application
Dim olNs As Namespace
Dim FlDr As MAPIFolder
Dim Subfldr As Outlook.Folder
Dim olMail As Outlook.MailItem
DIm olItems As Outlook.Items
Dim k As Integer

    Set olApp = New Outlook.Application
    Set olNs = olApp.GetNamespace ("MAPI")
    Set FlDr = olNs.GetDefaultFolder(olFolderIndex)
    Set Subfldr = FlDr.Folders("Subfolder_name")
    Set olItems = Subfldr.Items

    olItems.Sort "[Received]", True

    For k = 1 to olItems.Count
    Set olMail = olItems(k) 'this is where I get the error
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Maybe you have an item in the folder that is not a mail item. When the error occurs you need to do something like go to the immediate window and type

VBA Code:
print TypeName(olItems(k))
 
Upvote 0
Solution
That helped! Seems I had an 'undeliverable' notification from a sent email and it was stored as a ReportItem. After deleting the code is working as expected - thanks!
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,913
Members
449,093
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top