访问 - 对象变量或未设置块

时间:2016-08-29 06:39:12

标签: email ms-access outlook access-vba

我有一个问题系统,用户可以在其中打开门票并输入评论。这些表是SQL链接表。

我正在尝试通过添加Button来实现电子邮件的自动提取内容。

基本上,它将转到一个特定的文件夹" Pendings"在Outlook中,获取所有未标记为"已复制"。

的电子邮件

这是代码。

Public Sub myinbox()
Dim TempRst 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 Olfolder As Outlook.MAPIFolder


'MsgBox "It will take some time. So, Hang On!"

Set db = CurrentDb

Set Olapp = CreateObject("Outlook.Application")
Set Inbox = Olapp.GetNamespace("Mapi").GetFolderFromID("000000001A447390AA6611CD9BC800AA002FC45A03003683A021347CC54C82688B880BB383EC000000B95F710000") ' working folder ID

'
Set InboxItems = Inbox.Items
Set TempRst = CurrentDb.OpenRecordset("working") ' Table

For Each Mailobject In InboxItems
    'If Mailobject.UnRead Then
    If Mailobject.Categories <> "Copied" Then

    With TempRst

        .AddNew

        !Title = Mailobject.Subject
       ' !From = Mailobject.SenderName
       ' !To = Mailobject.To
       ' !Body = Mailobject.Body
        !OpenedDate = Mailobject.ReceivedTime
        '!email = Mailobject.SenderEmailAddress
        !OpenedBy = "Group1"
        !Priority = "(2) Normal"
        !Status = "Pending"
        .Update
        Mailobject.Categories = "Copied"
        Mailobject.Save
        Mailobject.UnRead = False


    End With

End If
Next

Set Olapp = Nothing
Set Inbox = Nothing
Set InboxItems = Nothing
Set Mailobject = Nothing
Set TempRst = Nothing

MsgBox "Emails updated successfully"

End Sub

上面的代码与Local表(我在本地测试而不链接到SQL)一起工作正常。但是,当我尝试使用Linked SQL表运行相同的代码时。我收到了这个错误:

  

错误:&#34;对象变量或未设置块&#34;

在这一行:

Set TempRst = CurrentDb.OpenRecordset("working")

1 个答案:

答案 0 :(得分:0)

这应该有效:

Set TempRst = db.OpenRecordset("SELECT * FROM working WHERE <yourPkField> IS NULL", dbOpenDynaset, dbSeeChanges)

您需要将dbSeeChanges与SQL Server链接表一起使用。由于您只是添加记录,因此无需选择任何现有记录。因此,where子句。