【问题标题】:type mismatch error on loop in vbavba循环中的类型不匹配错误
【发布时间】:2020-06-23 15:32:23
【问题描述】:

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

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

【问题讨论】:

  • 并非ItemsToProcess 中的所有内容都必须是MailItem - 例如可能是日历邀请。您需要在处理之前检查每个项目的类型。
  • 你为什么不看看VBA中的'locals'窗口并浏览ItemsToProcess变量,看看第二项是否有什么不同。对我来说,类型不匹配意味着它没有返回Outlook.MailItem。你也可以试试Dim oitem As Object,看看它是否能通过类型不匹配错误,并允许你检查 oitem 对象。这不是修复,只是帮助调试。

标签: vba outlook


【解决方案1】:
Dim oitem As Object   'not Outlook.MailItem
'....
For Each oitem In ItemsToProcess
    if typename(oitem)="MailItem" then
        'process the mail
        '....
    end if
Next oitem
'........

【讨论】:

  • 我突然想到前两个 MailItem 是使用 Outlook 创建的,但第三个 MailItem 最初是从我的 GMail 帐户生成的。这有什么区别吗?微软在这里又是专有的,只识别 Outlook 生成的电子邮件吗?我将在今天晚些时候尝试“Dim oitem as Object”声明和 typename(...),然后告诉你结果如何。
  • 嗯,我尝试了你的建议,它们都奏效了。谢谢大家。
猜你喜欢
  • 1970-01-01
  • 2012-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-05
  • 1970-01-01
  • 2017-04-12
相关资源
最近更新 更多