Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
366 views
in Technique[技术] by (71.8m points)

excel - Unable to find 'Type Mismatch Error" in VBA

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

question from:https://stackoverflow.com/questions/65846044/unable-to-find-type-mismatch-error-in-vba

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...