问题
I have Microsoft Office 365 Business installed on my PC and am attempting to run Access VBA to create an Excel Object. This is my Access VBA syntax I am using
Dim xl As Object, wb As Object, ws As Object, ch As Object
Set xl = CreateObject("Excel.Application")
However, it hits the CreateObject line and throws the image below, but ONLY on my PC running Office 365 Business. If I run this same syntax on a computer running Office 2010 it executes exactly as it should and creates the Excel Object error free.
What must I change in order to be able to run this syntax with Microsoft Office 365 Business?
EDIT
This is the only Registry Key that I see - it is close, but not exactly what was stated in the comments.
回答1:
One option would be to check to see if the CLSID exists before trying to call CreateObject. You can use the ole32.dll function CLSIDFromString to test it (this is also used by VBA internally for CreateObject and GetObject calls):
'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
You can wrap it in a simple "exists" test something like this:
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
This lets you test for classes before you try to create them (and avoids using the error handler to catch bad CLSIDs):
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
来源:https://stackoverflow.com/questions/41405937/active-x-error-with-excel-2016-and-late-binding