Excel 不同版本office的VBA会计参考

Excel 不同版本office的VBA会计参考,excel,vba,powerpoint,Excel,Vba,Powerpoint,如果用户使用64位Office和Office 16,下面的代码可以正常工作,但是如何考虑不同版本和32位的用户 Sub Add_References_Programmatically() Dim VBAEditor As Object Dim vbProj As Object Dim chkRef As Object Set VBAEditor = Application.VBE Set vbProj = ActivePresentation.V

如果用户使用64位Office和Office 16,下面的代码可以正常工作,但是如何考虑不同版本和32位的用户

Sub Add_References_Programmatically()
Dim VBAEditor       As Object
Dim vbProj          As Object
Dim chkRef          As Object

Set VBAEditor = Application.VBE
Set vbProj = ActivePresentation.VBProject

On Error Resume Next
    vbProj.References.AddFromGuid "{0002E157-0000-0000-C000-000000000046}", 5, 0
On Error GoTo 0

For Each chkRef In vbProj.References
    If chkRef.Name = "Excel" Then
        GoTo CleanUp
    End If
Next chkRef

vbProj.References.AddFromFile "C:\Program Files\Microsoft Office\Root\Office16\EXCEL.EXE"

CleanUp:
Set vbProj = Nothing
Set VBAEditor = Nothing
End Sub

将相关代码放入编译器指令中。或者使用后期绑定(
GetObject(FName)
CreateObject(AppName)
)。然后在运行时绑定到函数的名称。它比较慢,但通常很快,你不会注意到。我喜欢的另一个对编译器指令的引用