无法弄清楚哪些对象被声明为非SET

时间:2016-06-28 13:23:24

标签: vba ms-access access-vba

我的Access VBA脚本中出现错误,指出错误#91:对象变量或未设置块变量。我在MSDN网站上搜索了found the reference。它说我要么宣布一个对象不是SET,要么我有一个WITH块变量而不是SET。看到我的脚本中没有WITH块,它必须是第一个。但是对于我仍然相当幼稚的眼睛,我正在设置我正在使用的所有物体。有人可以帮我解决这个错误吗?

侧面任务:我是否正确处理错误?我添加了If语句,因为它每次都会显示0的错误消息,因此我将其设置为只显示Err.Number > 0是否发生。

Private Sub Form_Timer()


On Error GoTo ErrorHandler

    current_date_time = Now


    If current_date_time = #6/28/2016 8:18:15 AM# Then 

        MsgBox ("the current_date_time variable holds: " & current_date_time)


        Dim dbs As DAO.Database
        Dim rst As DAO.Recordset
        Dim qdf As DAO.QueryDef
        Dim mail_body As String
        Dim oApp As Outlook.Application
        Dim oMail As MailItem

        mail_body = "The following jobs...blah...blah..." & vbCrLf

        Set dbs = CurrentDb
        Set qdf = dbs.QueryDefs("qry_BMBFLoc")
        Set rst = qdf.OpenRecordset

        If Not (rst.EOF And rst.BOF) Then

            rst.MoveFirst
            Do Until rst.EOF = True

                    mail_body = mail_body & rst!job & "-" & rst!suffix & vbCrLf

            rst.MoveNext
            Loop

        End If

        rst.Close
        dbs.Close

        Set rst = Nothing
        Set oMail = oApp.CreateItem(olMailItem)

            oMail.Body = mail_body
            oMail.Subject = "Blah blah"
            oMail.To = "someone@someplace.com" 
            oMail.Send

        Set oMail = Nothing
        Set oApp = Nothing

        End If

ErrorHandler:

    If (Err.Number > 0) Then
        MsgBox ("Error #: " & Err.Number & vbCrLf & vbCrLf & Err.Description)
    End If

End Sub

1 个答案:

答案 0 :(得分:1)

首先,我会有以下几行:

Dim newnode As TreeNode
Dim dirs As New DirectoryInfo(node.FullPath)
For Each dir As DirectoryInfo In dirs.GetDirectories()
    If dir.Attributes = FileAttributes.Directory Then

    Else

    End If
Next

然后,您需要在设置dim oMail as outlook.mailItem

之前设置OApp
oMail

如果您发现自己在发送操作上遇到运行时错误287,请参阅我的帖子here

我会使用的标准错误处理程序是这样的:

Set oApp = CreateObject("Outlook.application")