Runtime error 13: Type mismatch for loops

nuckfuts

New Member
Joined
Mar 10, 2020
Messages
43
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

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

6StringJazzer

Well-known Member
Joined
Jan 27, 2010
Messages
846
Office Version
  1. 365
Platform
  1. Windows
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))
 
Solution

nuckfuts

New Member
Joined
Mar 10, 2020
Messages
43
Office Version
  1. 365
Platform
  1. Windows
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!
 

Watch MrExcel Video

Forum statistics

Threads
1,130,290
Messages
5,641,368
Members
417,206
Latest member
sam89665

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
Top