【问题标题】:Unable to find 'Type Mismatch Error" in VBA在 VBA 中找不到“类型不匹配错误”
【发布时间】:2021-04-26 23:20:32
【问题描述】:

它在我登录系统的第一次工作,但如果我要再次运行此代码进行第二次测试,它会给我类型不匹配错误。有人可以帮我解决这个问题吗?

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

错误截图:https://i.stack.imgur.com/5dKPy.png

【问题讨论】:

  • Var 是一个String,看起来像一个Date。您将它与实际的Date 进行比较 - 所以请改用If CDate(var) &lt; Date Then。 - And var = Date And 相同
  • 您的屏幕截图显示不同的错误。
  • 对于初学者来说,并非收件箱中的每个项目都一定是MailItem: stackoverflow.com/questions/78924/…

标签: excel vba outlook


【解决方案1】:

这可以帮助您忽略类型不匹配错误:

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

【讨论】:

    【解决方案2】:

    收件箱中可以有邮件以外的项目。

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-12
      • 2017-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多