0

Its works frist time when i am login to the system but for testing second time if i am going to run this code again it will gives me type mismatch error. can somebody help me on this.

Sub Saveattachment()
    Application.DisplayAlerts = False
    
    Dim ATMT As Outlook.Attachment
    Dim OMAIL As Outlook.MailItem
    Dim FOL As Outlook.Folder
    Dim ONS As Outlook.Namespace
    Dim OLOOK As Outlook.Application
    Dim var As Date
    
    Dim count As Long
    count = 0
    
    Dim name As String
    Dim temp As Variant
    
    Set OLOOK = New Outlook.Application
    Set ONS = Outlook.GetNamespace("MAPI")
    Set FOL = ONS.Folders("IM_DMBI").Folders("inbox")
    Set OMAIL = OLOOK.CreateItem(olMailItem)
    
    msgbox "Please remove old downloads, If already remove please ingore and press Ok to proceed", vbInformation
    
    For Each OMAIL In FOL.items
        For Each ATMAT In OMAIL.Attachments
            var = Format(OMAIL.ReceivedTime, "MM/DD/YY")
            name = Left(OMAIL.Subject, 3)
    
            If name = "304" And var = Date And Err.Number = 13 Then
                count = count + 1
                ATMAT.SaveAsFile Sheet1.Cells(1, 1) & Application.PathSeparator & ATMAT.filename
            End If
    
            If var < Date Then
                msgbox "Totlay:-" & count & " Files downloaded for today", vbInformation
                Exit Sub
            End If
        Next
    Next

    Application.DisplayAlerts = True
End Sub

Screenshot of the error: https://i.stack.imgur.com/5dKPy.png

  • 2
    `Var` is a `String` that looks like a `Date`. You are comparing it to an actual `Date` - So use `If CDate(var) < Date Then` instead. - same for `And var = Date And` – braX Jan 22 '21 at 13:29
  • 3
    Your screenshot shows a different error. – GSerg Jan 22 '21 at 13:56
  • 1
    For starters, not every item in an inbox is necessarily a `MailItem`: https://stackoverflow.com/questions/78924/when-is-a-mailitem-not-a-mailitem – BigBen Jan 22 '21 at 14:11

2 Answers2

0

This helps you to ignore type mismatch error:

Sub GetAttachment()
    Application.DisplayAlerts = False
    Dim ATMT As Outlook.Attachment
    Dim OMAIL As Outlook.MailItem
    Dim FOL As Outlook.Folder
    Dim ONS As Outlook.Namespace
    Dim OLOOK As Outlook.Application
    Dim var As Date
    Dim count As Long
    count = 0
    Dim name As String
    Dim temp As Variant
    Set OLOOK = New Outlook.Application
    Set ONS = Outlook.GetNamespace("MAPI")
    Set FOL = ONS.Folders("IM_DMBI").Folders("inbox")
    Set OMAIL = OLOOK.CreateItem(olMailItem)
    'msgbox "Please remove old downloads, If already remove please ingore and press Ok to proceed", vbInformation
    For Each OMAIL In FOL.items
        On Error GoTo errorHandler
        For Each ATMAT In OMAIL.Attachments
            var = Format(OMAIL.ReceivedTime, "MM/DD/YY")
            name = Left(OMAIL.Subject, 5)
            
            If name = "304 r" And var = Date Then
                count = count + 1
                ATMAT.SaveAsFile Sheet1.Cells(1, 1) & Application.PathSeparator & ATMAT.filename
            End If
            
            If var < Date Then
                'msgbox "Totlay:-" & count & " Files downloaded for today", vbInformation
                Exit Sub
            End If
        Next
        TypeMismatch:
        Next
    errorHandler:
    If Err = 13 Then        'Type Mismatch
        Resume TypeMismatch
    End If
    Application.DisplayAlerts = True
End Sub
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
0

There can be items other than mailitems in the inbox.

Option Explicit ' Consider this mandatory
' Tools | Options | Editor tab
' Require Variable Declaration
' If desperate declare as Variant


Sub Saveattachment()

    Application.DisplayAlerts = False
    
    'Dim ATMT As outlook.Attachment
    Dim ATMAT As outlook.Attachment
    
    Dim oObjItem As Object  'Any type, there can never be a mismatch
    Dim OMAIL As outlook.MailItem
    
    Dim FOL As outlook.folder
    Dim ONS As outlook.namespace
    Dim OLOOK As outlook.Application
    Dim var As Date
    
    Dim count As Long
    count = 0
    
    Dim name As String
    Dim temp As Variant
    
    Set OLOOK = New outlook.Application
    
    'Set ONS = outlook.GetNamespace("MAPI")
    Set ONS = OLOOK.GetNamespace("MAPI")
    
    Set FOL = ONS.folders("IM_DMBI").folders("inbox")
    
    'Set OMAIL = OLOOK.CreateItem(olMailItem)
    
    For Each oObjItem In FOL.Items ' any type of item
    'For Each OMAIL In FOL.Items
    
        ' One of at least three ways to verify
        If TypeName(oObjItem) = "MailItem" Then
        
            ' Now that you have a mailitem
            Set OMAIL = oObjItem
            
            For Each ATMAT In OMAIL.Attachments
                Debug.Print "Attachment found."
            Next
            
        Else
            Debug.Print "This would have been a type mismatch."
            
        End If
        
    Next
    
    Application.DisplayAlerts = True
    
End Sub
niton
  • 8,771
  • 21
  • 32
  • 52