我有一个简单的 Excel 文件,它在数据库打开时查询数据库,然后自动关闭。
- 如果我在 Windows 资源管理器(我运行的是 Windows XP 和 Excel 2002)中双击文件打开它,它工作正常。
- 但是,如果我使用
Excel "C:\DataUpdate.xls"
从 Start > Run 或从Shell
在单独文件中的另一个 Excel 实例中运行它, Excel 将在退出时崩溃。
我不明白为什么它在一种情况下会这样做,而在另一种情况下却不会。
在 DataUpdate.xls 中,我有 2 个过程,UpdateTable()
和 OnWorkbookOpen()
,当工作簿打开。
Option Explicit
Sub UpdateTable()
Dim ws As Worksheet
Dim qt As QueryTable
Set ws = Worksheets("Sheet1")
Set qt = ws.Range("A1").QueryTable
qt.Refresh BackgroundQuery:=False
End Sub
Sub OnWorkbookOpen()
On Error Resume Next
If ActiveWorkbook.Name = "DataUpdate.xls" Then
'I put this If statement in so I can change the file's
'name and then edit the file without code
'running.
UpdateTable
ActiveWorkbook.Save
Excel.Application.Quit
End If
End Sub
如果我从 Windows 资源管理器打开文件,一切都很好。如果我使用 Excel "C:\DataUpdate.xls"
从命令行运行它,代码运行正常,直到应用程序尝试使用 Application.Quit
退出,此时点 Excel 抛出异常。
当我查看错误报告的详细信息时,这是我找到的一些信息:
AppName: excel.exe
AppVer: 10.0.6854.0
ModName: olconnector.dll
ModVer: 2.0.2303.0
Offset: 000114d5
为什么根据我启动应用程序的方式会有不同的行为,以及我该怎么做才能使应用程序的行为相同,而不管我如何启动它?
最佳答案
根据 google olconnector.dll 是 outlook connector,一个 office 插件。
http://ask.officelive.com/workspace/qna/t/4578.aspx
由于它处理的是 Outlook,我相信它会对您在计算机上使用的登录有一定的依赖性。
当您打开 excel 时,您使用的凭据是否与您登录计算机所用的帐户相同?
ProcessStartInfo startInfo = null;
Process batchProcess = null;
startInfo = new ProcessStartInfo();
startInfo.Domain = "somedomain";
startInfo.UserName = "Domainuser";
startInfo.Password = "pwd";
不太确定错误是什么,但这是您可以尝试的。
关于windows - 为什么从命令行启动时 Excel 在退出时抛出异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1716815/