0

I below code to replyall to email shows

"Run-time error 13 type mismatch"

when it runs to Next (for each next loop).

I set up the rule for email received today. When it comes to next (next date), it shows the error message.

I debugged, it stopped the error message until the received-time is today.

sub fwdmail ()
dim i as long
dim otlk as outlook.application
dim nmspc as outlook.namespace
dim olmail as Outlook.MailItem
dim objfolder as Outlook.MAIPfolder
dim oreply as Outlook.MailItem

set otlk=New Outlook.Applicaiton
Set Nmsp=otlk.GetNamespace("MAPI")
Set objfolder=nmspc.getdefaultFolder("olFolderInbox).Folder("notice")
for each olmail in objfolder.Items
  if olmail.ReceivedTime>=Format(Date, "YYYY/MM/DD") then
    ' do the stuff here
  end if
next

I tired to check if a mailitem type, but got the same error message.

for each olmail in objfolder.Items
   if typeof olmail is outlook.item then

     if olmail.ReceivedTime>=Format(Date, "YYYY/MM/DD") then
     ' do the stuff here
      end if
   end if
next
Community
  • 1
  • 1
mei
  • 21
  • 5
  • Most probably you have an item in that folder that is not a mailitem, see [here](https://stackoverflow.com/questions/78924/when-is-a-mailitem-not-a-mailitem). – BigBen Dec 06 '19 at 15:56
  • i fixed the typo on questions. I have also tried to check if it is a mailitem in IF typeof , but i still got the error message> – mei Dec 06 '19 at 16:35
  • You need to check the `TypeOf` the item *before* trying to loop. – BigBen Dec 06 '19 at 17:52
  • 1
    Note that you can use [`Items.Restrict`](https://learn.microsoft.com/en-us/office/vba/api/outlook.items.restrict) instead of looping. – BigBen Dec 06 '19 at 17:53
  • Also - `MailItem.ReceivedTime` is a `Date`, don't compare it to a `Variant/String`, which is what `Format` returns. – BigBen Dec 06 '19 at 17:55

2 Answers2

1

Declare a variable that allows all types of items. Test that to see if it is a mailitem.

Option Explicit ' Consider this mandatory
' Tools | Options | Editor tab
' Require Variable Declaration

Sub fwdmail()

Dim olNmspc As NameSpace
Dim olFolder As folder

Dim olItm As Object         ' <-- Items may not be mailitems
Dim olMitm As MailItem

Set olNmspc = GetNamespace("MAPI")

' Multiple lines allows easier troubleshooting
Set olFolder = olNmspc.GetDefaultFolder(olFolderInbox)
Set olFolder = olFolder.folders("notice")

For Each olItm In olFolder.Items

    If TypeOf olItm Is MailItem Then    ' <-- test if item is a mailitem

        Set olMitm = olItm

        If olMitm.ReceivedTime >= Format(Date, "YYYY/MM/DD") Then
            ' do the stuff here
            Debug.Print "olMitm.Subject......: " & olMitm.Subject
            Debug.Print " olMitm.ReceivedTime: " & olMitm.ReceivedTime
            Debug.Print " Format date........: " & Format(Date, "YYYY/MM/DD")
            Debug.Print
        End If

    End If

Next

End Sub

niton
  • 8,771
  • 21
  • 32
  • 52
0

I would suggest comparing the format of the received time with the format you are comparing to.

wakgtech
  • 75
  • 6