错误91:对象变量或未设置块变量所需的帮助

时间:2017-09-16 04:58:10

标签: access-vba outlook-vba

我正在尝试使用以下给定的过程来下载唯一的邮件但是收到错误(91对象变量或未设置块变量) 行错误:。FindFirst "task =""" & Mailobject.UserProperties.Find("taskID") & """"

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

3 个答案:

答案 0 :(得分:1)

此错误指的是记录集对象中的某些错误(它未正确设置,或者在.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 :(得分:1)

尝试替换

Set rst= CurrentDb.OpenRecordset("mls")

通过

Set rst= db.OpenRecordset("mls")

答案 2 :(得分: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