Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
804 views
in Technique[技术] by (71.8m points)

ms access - Active X Error With Excel 2016 And Late Binding

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?

Error Message

EDIT
This is the only Registry Key that I see - it is close, but not exactly what was stated in the comments.
Registry Key

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...