0
votes

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
2
Most probably you have an item in that folder that is not a mailitem, see here.BigBen
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
You need to check the TypeOf the item before trying to loop.BigBen
Note that you can use Items.Restrict instead of looping.BigBen
Also - MailItem.ReceivedTime is a Date, don't compare it to a Variant/String, which is what Format returns.BigBen

2 Answers

0
votes

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

0
votes

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