在过去的几个小时中,我一直在努力解决此问题,并尝试了多种解决方案,但无济于事。 我正在尝试在MS Access中打开excel 2016(64位)报告,默认excel是2003,现在必须保持这种状态。 我的代码是:
Dim xlTmp As excel.Application
Shell ("C:\Program Files\Microsoft Office\root\Office16\EXCEL.exe")
Set xlTmp = GetObject(, "Excel.Application")
仅当我在调试模式下逐步执行时,此代码才能完全满足我的要求,因为excel在尝试获取对象之前并未完全启动。 如果运行正常,则会引发错误:
运行时错误“ 429” ActiveX组件无法创建对象
我尝试了以下一些解决方案,但无济于事:
Function OpenExcel()
x = Shell("C:\Program Files\Microsoft Office\root\Office16\EXCEL.exe", vbNormalFocus)
OpenExcel = x
End Function
Function GetOpenExcel() As excel.Application
Set GetOpenExcel = GetObject(, "Excel.Application.16")
TryAgain:
On Error GoTo NoExcel
Set xlTmp = GetObject(, "Excel.Application.16")
On Error GoTo 0
xlTmp.Visible = True
Set GetOpenExcel = xlTmp
Exit Function
NoExcel:
Resume TryAgain
End Function
还有这个
Set ie = Nothing
cnt = 0
cnt = xlTmp.Windows.count
sh = Shell("C:\Program Files\Microsoft Office\root\Office16\EXCEL.exe", vbNormalFocus)
Do Until xlTmp.Windows.count = cnt + 1
Loop
Set xlTmp = GetObject("excel.Application.16")
Set sh = xlTmp.Windows(xlTmp.Windows.count - 1)
我还尝试了一些切换,有些混乱,但这是我目前的情况: MS Access References
在此先感谢您的帮助。
答案 0 :(得分:0)
只需使用
Set xlTmp = CreateObject("Excel.Application")
代替GetObject
更新
此代码对我有用,我有默认的2016年而不是默认的2010年:
Private Sub Command0_Click()
Dim xlTmp As Object
Shell "D:\Program Files (x86)\Microsoft Office 2010\Office14\EXCEL.EXE", vbNormalFocus
TryAgain:
On Error GoTo NoExcel
Set xlTmp = GetObject(, "Excel.Application")
On Error GoTo 0
'here I received correct xlTmp
Exit Sub
NoExcel:
Resume TryAgain
End Sub