2012-11-29 44 views
1

我在Outlook VBA中工作,並構建了一個For Next循環來讀取格式類似於Key = Value對的MailItems主體。似乎有點工作,但在第二次迭代到達「Next oitem」時,我得到錯誤「type mismatch」。那麼,還有第三個MailItem被讀入,所以我不知道爲什麼我得到這個錯誤。任何指導將不勝感激。在vba中循環類型不匹配錯誤

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 
+0

在'ItemsToProcess'必然將是一個'MailItem'不是一切 - 可能是一個日曆邀請的例子。處理之前,您需要檢查每個項目的類型。 –

+0

爲什麼不看看VBA中的'locals'窗口並瀏覽'ItemsToProcess'變量,看看第二個項目是否有任何不同。對我來說,類型不匹配意味着它不會返回一個'Outlook.MailItem'。你也可以嘗試'Dim oitem As Object',看看它是否會超過類型不匹配的錯誤,並允許你檢查oitem對象。這不是一個修復,只是一個調試的幫助。 –

回答

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

+1這是一個常見問題。 – brettdj

+0

剛纔我發現前兩個MailItem是使用Outlook創建的,但第三個MailItem最初是從我的GMail帳戶生成的。這有什麼區別嗎?微軟是否在這裏再次專有,只能識別由Outlook生成的電子郵件?我將在今天晚些時候嘗試「Dim oitem as Object」聲明和類型名稱(...),並讓您知道這是如何解決的。 – Alan

+0

嗯,我嘗試了你的建議,他們都工作。多謝你們。 – Alan