2017-09-16 72 views
0

我試圖使用下面的給定過程來下載唯一的郵件,但出現錯誤(91對象變量或塊變量未設置) 錯誤行:。 FindFirst "task =""" & Mailobject.UserProperties.Find("taskID") & """"錯誤91:對象變量或塊變量未設置需要幫助

Private Sub getml() 
Dim rst As DAO.Recordset 
Dim OlApp As Outlook.Application 

Dim inbox As Outlook.MAPIFolder 
Dim inboxItems As Outlook.Items 
Dim Mailobject As Object 
Dim db As DAO.Database 
Dim dealer As Integer 
Set db = CurrentDb 

Set OlApp = CreateObject("Outlook.Application") 
Set inbox = OlApp.GetNamespace("Mapi").GetDefaultFolder(olFolderInbox) 
Set rst= CurrentDb.OpenRecordset("mls") 
Set inboxItems = inbox.Items 
For Each Mailobject In inboxItems 

    With rst 
     .FindFirst "task =""" & Mailobject.UserProperties.Find("taskID") & """" 
     If .NoMatch 
      .AddNew 
      !task= Mailobject.UserProperties.Find("taskID") 
      !tsktml= Mailobject.UserProperties.Find("timeline") 
      .Update 

      Mailobject.UnRead = False 
     End If 
    End With 
End If 
Next 
Set OlApp = Nothing 
Set inbox = Nothing 
Set inboxItems = Nothing 
Set Mailobject = Nothing 

End Sub 
+0

我試圖調試代碼並找出錯誤。變體var在搜索完所有郵件後顯示空值。即var返回taskid直到最後一封郵件,但在搜索最後一封郵件之後,而不是退出for語句代碼後再次循環郵件並顯示錯誤。所以我認爲我需要申請一個出口來幫助你應用它。 – Supernova

回答

1

此錯誤是指一些bug在你的記錄集對象(它要麼沒有設置正確,或者.FindFirst運行之前已關閉)。

我無法使用您提供的代碼進行復制,因此您需要自行解決此問題。

您可以經常通過刪除With塊得到更多的描述性錯誤:

For Each Mailobject In inboxItems 
    rst.FindFirst "task =""" & Mailobject.UserProperties.Find("taskID") & """" 
    If rst.NoMatch 
     rst.AddNew 
     rst!task= Mailobject.UserProperties.Find("taskID") 
     rst!tsktml= Mailobject.UserProperties.Find("timeline") 
     rst.Update 

     Mailobject.UnRead = False 
    End If 
End If 
1

嘗試更換

Set rst= CurrentDb.OpenRecordset("mls") 

通過

Set rst= db.OpenRecordset("mls") 
+0

我試過這個,但仍然收到錯誤 – Supernova

0

最後,這是我已經處理了錯誤

Private Sub getml() 
Dim rst As DAO.Recordset 
Dim OlApp As Outlook.Application 

Dim inbox As Outlook.MAPIFolder 
Dim inboxItems As Outlook.Items 
Dim Mailobject As Object 
Dim db As DAO.Database 
Dim var As variant 
Set db = CurrentDb 

Set OlApp = CreateObject("Outlook.Application") 
Set inbox = OlApp.GetNamespace("Mapi").GetDefaultFolder(olFolderInbox) 
Set rst= CurrentDb.OpenRecordset("mls") 
Set inboxItems = inbox.Items 
On error resume next 
For Each Mailobject In inboxItems 
    set var = MailObject.UserProperties.Find("taskID") 
IF Not (var Is Nothing) Then 
     With rst 
      .FindFirst "task=" Chr(34) & var & Chr(34) 
     If .NoMatch then 
      .AddNew 
      !task= var.value & "" 
      .Update 

      Mailobject.UnRead = False 
     End If 
    End With 
End If 
Next 
Set OlApp = Nothing 
Set inbox = Nothing 
Set inboxItems = Nothing 
Set Mailobject = Nothing 
End sub 
相關問題