Excel 2016中的活动X错误和后期绑定

我的电脑上安装了Microsoft Office 365 Business ,并试图运行Access VBA来创buildExcel对象。 这是我使用的Access VBA语法

 Dim xl As Object, wb As Object, ws As Object, ch As Object Set xl = CreateObject("Excel.Application") 

但是,它碰到了CreateObject行并抛出下面的图像,但在我的PC运行Office 365业务。 如果我在运行Office 2010的计算机上运行此相同的语法,则它将执行完全相同的操作,并自由创buildExcel对象错误。

为了能够使用Microsoft Office 365 Business运行此语法,必须更改什么?

错误信息

编辑
这是我看到的唯一的registry密钥 – 它接近,但不完全是评论中所述。
注册表项

一种select是在尝试调用CreateObject之前检查CLSID是否存在。 您可以使用ole32.dll函数CLSIDFromString来testing它(VBA在内部也使用这个函数来调用CreateObjectGetObject ):

 'Note, this is the 32bit call - use PtrSafe for 64bit Office. Private Declare Function CLSIDFromString Lib "ole32.dll" _ (ByVal lpsz As LongPtr, ByRef pclsid As LongPtr) As Long 

你可以把它包装在一个简单的“存在”testing中,像这样:

 Private Function ClassIdExists(clsid As String) As Boolean Dim ptr As LongPtr Dim ret As Long ret = CLSIDFromString(StrPtr(clsid), ptr) If ret = 0 Then ClassIdExists = True End Function 

这使您可以尝试创build类之前testing类(并避免使用error handling程序来捕获错误的CLSID):

 Dim xl As Object If ClassIdExists("Excel.Application") Then Set xl = CreateObject("Excel.Application") ElseIf ClassIdExists("Excel.Application.16") Then Set xl = CreateObject("Excel.Application.16") Else MsgBox "Can't locate Excel class." End If