在 vba 中循环输入不匹配错误 [英] type mismatch error on loop in vba

查看:46
本文介绍了在 vba 中循环输入不匹配错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Outlook VBA 中工作并构建了一个 For Next 循环来读取格式为 Key=Value 对的 MailItems 的正文.在某种程度上它似乎正在工作,但是在第二次迭代结束时,当它到达下一个 oitem"时,我得到了抛出类型不匹配"的错误.嗯,还有第三个 MailItem 需要读入,所以我不知道为什么我会收到这个错误.任何指导将不胜感激.

I'm working in Outlook VBA and have constructed a For Next loop to read in the body of MailItems which are formatted like Key=Value pairs. To a point it seems to be working, but on the end of the second iteration when it reaches the "Next oitem" I get the error thrown "type mismatch". Well,there is still a third MailItem to be read in, so I don't know why I am getting this error. Any guidance would be appreciated.

Sub ReadMailItems()

Dim olapp As Outlook.Application
Dim olappns As Outlook.NameSpace
Dim oitem As Outlook.MailItem
Dim ItemsToProcess As Outlook.Items
Dim myFolder As MAPIFolder
Dim sFilter As String
Dim dailyStats As CRBHA_Stats
Dim kvPairs As Collection
Dim Item As KeyValuePair
Dim today As Date
today = Date

On Error GoTo LocalErr

'set outlook objects
Set olapp = New Outlook.Application
Set olappns = olapp.GetNamespace("MAPI")
Set myFolder = olappns.GetDefaultFolder(olFolderInbox)
'Filter or only MailItems received today
sFilter = "[ReceivedTime] >= " & AddQuotes(Format(Date, "ddddd"))
Set ItemsToProcess = Session.GetDefaultFolder(olFolderInbox).Items.Restrict(sFilter)
Set StatsCollection = New Collection

For Each oitem In ItemsToProcess
 If CheckSubject(oitem.Subject) Then
   Set kvPairs = GetKeyValuePairs(oitem.body)
   'Iterate over the Collection and load up
   'an instance of CRBHA_Stats object
   Set dailyStats = New CRBHA_Stats
   dailyStats.SubmissionDate = today
   For Each Item In kvPairs
     If LCase(Item.Key) = LCase("EmployeeID") Then
        dailyStats.EmployeeID = Item.Value
     ElseIf LCase(Item.Key) = LCase("Approved") Then
        dailyStats.Approved = Item.Value
     ElseIf LCase(Item.Key) = LCase("Declined") Then
        dailyStats.Declined = Item.Value
     ElseIf LCase(Item.Key) = LCase("PFA") Then
        dailyStats.PFAs = Item.Value
     ElseIf LCase(Item.Key) = LCase("Followups") Then
        dailyStats.FollowUps = Item.Value
     ElseIf LCase(Item.Key) = LCase("CRA") Then
        dailyStats.CRAs = Item.Value
     End If
    Next Item

    'Add each CRBHA_Stats object to the StatsCollection
    StatsCollection.Add dailyStats

    Debug.Print dailyStats.ToString
    Debug.Print "_____________" & vbCrLf
  End If
Next oitem   '<<<<This is where it cuts out

ExitProc:
Set olapp = Nothing
Set olappns = Nothing
Set myFolder = Nothing
Set ItemsToProcess = Nothing
Set dailyStats = Nothing
Exit Sub

LocalErr:
  If Err.Number <> 0 Then
   Msg = "Error # " & Str(Err.Number) & " was generated by " _
       & Err.Source & Chr(13) & "Error Line: " & Erl & Chr(13) & Err.Description
   MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
   End If
   'Resume Next


End Sub

推荐答案

Dim oitem As Object   'not Outlook.MailItem
'....
For Each oitem In ItemsToProcess
    if typename(oitem)="MailItem" then
        'process the mail
        '....
    end if
Next oitem
'........

这篇关于在 vba 中循环输入不匹配错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆