使用 AHK 关闭访问应用程序时未设置对象变量(错误 91)

Object variable not set (Error 91) when using AHK to close an Access Application

我有一个名为 "Form1" 的访问表单,在该表单中它有几个子项,例如 Form_Load、Form_Unload 等

我想用一个AutoHotKey脚本来控制窗体的打开和关闭。

表单打开代码:

acc:= ComObjCreate("Access.Application")
acc.OpenCurrentDatabase("\...\target_database.accdb")
acc.Visible := True
acc.UserControl := True
acc.DoCmd.OpenForm("Form1")

表单关闭代码:

acc.quit

Error 91

当它试图关闭应用程序时弹出。错误似乎与 Form_Unload 子有关。它只发生在我使用 AutoHotKey 时,我可以毫无问题地手动关闭 Access。

这是 Form_Unload 的代码:

Private Sub Form_Unload(Cancel As Integer)
' update the data in txt, signal and datetime
Dim fso As New FileSystemObject
'the file we're going to write to
Dim ts As TextStream
'open this file to write to it
Set ts = fso.CreateTextFile("\cp-apps01\ExtruderDataCollector\TESTING\log.txt", True)
For Each Key In Module1.dict_pre_Signal.Keys
    ts.WriteLine (Key & " " & Module1.dict_pre_Signal.Item(Key) & " " _
        & DateValue(CStr(Module1.dict_pre_Time.Item(Key))) & " " _
        & TimeValue(CStr(Module1.dict_pre_Time.Item(Key))))
Next Key
ts.Close
Set fso = Nothing
End Sub

有什么方法可以在关闭访问应用程序之前关闭表单或将表单更改为设计视图?

关闭表单怎么样?

acc.DoCmd.OpenForm("Form1")
acc.DoCmd.Close(acForm, "Form1", acSaveNo)
acc.Quit

或常量的数值:

acc.DoCmd.OpenForm("Form1")
acc.DoCmd.Close(2, "Form1", 2)
acc.Quit