如何在excel 2010中更改默认vba引用

如何在excel 2010中更改默认vba引用,excel,excel-2010,vba,Excel,Excel 2010,Vba,我想向personal.xlsb添加一个vba函数,但该函数中有一个ADODB.Connection对象 我可以通过(在VBA编辑器中)选择工具->引用,然后选中“Microsoft ActiveX数据对象6.0库”框来解决此问题 我的问题是,如何将“Microsoft ActiveX Data Objects 6.0 Library”作为默认参考之一,以便在启动excel时始终可用?而Siddharth的解决方案非常方便,我想我会从我推出的一个工具中提供这些功能,在这个工具中,我们经常收到与用

我想向personal.xlsb添加一个vba函数,但该函数中有一个ADODB.Connection对象

我可以通过(在VBA编辑器中)选择工具->引用,然后选中“Microsoft ActiveX数据对象6.0库”框来解决此问题


我的问题是,如何将“Microsoft ActiveX Data Objects 6.0 Library”作为默认参考之一,以便在启动excel时始终可用?

而Siddharth的解决方案非常方便,我想我会从我推出的一个工具中提供这些功能,在这个工具中,我们经常收到与用户没有在Excel版本的VBE中检查正确的引用相关的调用,原因可能有很多

只是把它作为一种选择扔掉,让它看起来更漂亮。它有一些非常特定于项目的特性,但是您可以很容易地修改它以满足您的需要

Function ValidateReferences() As Boolean
'makes sure that user has the proper references installed for downloading data through API

Dim arrDescription() As String, arrGUID() As String
Dim arrMajor() As Long, arrMinor() As Long
Dim strMessage As String


ValidateReferences = True

'removes any broken references
For i = ThisWorkbook.VBProject.References.Count To 1 Step -1
    If ThisWorkbook.VBProject.References.Item(i).IsBroken Then ThisWorkbook.VBProject.References.Remove ThisWorkbook.VBProject.References.Item(i)
Next

'will attempt to add the reference for the user
arrGUID() = Split("{2A75196C-D9EB-4129-B803-931327F72D5C},{E6C9285A-7A87-407A-85E7-D77A70C100F5},{45A929B3-E493-4173-B6E5-0CD42041C6DC},{F24B7FA2-8FB9-48B7-825F-7C9F4A82F917},{7A80DAB5-1F61-4F9A-A596-561212ACD705},{18142BD6-1DE1-412B-991C-31C7449389E6},{C3ED6DC2-BED0-4599-A170-B1E1E32C627A}", ",", , vbTextCompare) ' {2A75196C-D9EB-4129-B803-931327F72D5C}
arrDescription() = Split("Microsoft ActiveX Data Objects 2.8 Library,myDummyRef COM 3.8.0,myDummyRef COM 3.8.0,myDummyRef COM 3.8.0,myDummyRef 3.6.0 Library,myDummyRef,myDummyRef Library", ",", , vbTextCompare) 'Microsoft ActiveX Data Objects 2.8 Library
ReDim arrMajor(6)
    arrMajor(0) = 0 '0
    arrMajor(1) = 3 '3
    arrMajor(2) = 3 '3
    arrMajor(3) = 3 '3
    arrMajor(4) = 3 '3
    arrMajor(5) = 1 '1
    arrMajor(6) = 1 '1 -> ADODB = 2
ReDim arrMinor(6)
    arrMinor(0) = 0
    arrMinor(1) = 8
    arrMinor(2) = 8
    arrMinor(3) = 8
    arrMinor(4) = 6
    arrMinor(5) = 0
    arrMinor(6) = 0 '-> ADODB = 8

For i = LBound(arrGUID()) To UBound(arrGUID())
    On Error GoTo ErrCheck
    If i = 0 Then 'adodb not working on AddFromFile. I could use add from file on all references, perhaps? -> refactor later
        ThisWorkbook.VBProject.References.AddFromFile ("C:\Program Files\Common Files\System\ado\msado15.dll")
    Else
        ThisWorkbook.VBProject.References.AddFromGuid arrGUID(i), arrMajor(i), arrMinor(i)
    End If
Next

If ValidateReferences = False Then MsgBox "The following references could not be added to the VB Project: " & Right(strMessage, Len(strMessage) - 2) & "." & vbNewLine & vbNewLine & "Please refer to 4. Appropriate DLL Files on the 'Connectivity_Help' tab for more information."

Exit Function

ErrCheck:
    Select Case Err.Number
        Case Is = 32813 'reference already in use, nothing to report
        Case Else
            ValidateReferences = False
            strMessage = strMessage & ", " & arrDescription(i)
    End Select
    Resume Next

End Function
此函数将打印您的引用信息,以帮助您找到它的dll,但必须检查引用才能打印它

Sub print_ref_path()

Dim i As Integer

For i = ThisWorkbook.VBProject.References.Count To 1 Step -1
    Dim strName As String, strPath As String, strGUID as String
    strName = ThisWorkbook.VBProject.References.Item(i).name
    Debug.Print strName
    strPath = ThisWorkbook.VBProject.References.Item(i).FullPath
    Debug.Print strPath
    strGUID = ThisWorkbook.VBProject.References.Item(i).GUID 'think this is right, might need to change
Next

End Sub

这里有另一种方法。可以添加为按钮或打开命令。不太干净,但用最少的代码完成工作:

Sub AddReference()

On Error GoTo ErrHandler:

'VBA
References.AddFromGuid "{000204EF-0000-0000-C000-000000000046}", 0, 0
'Access
References.AddFromGuid "{4AFFC9A0-5F99-101B-AF4E-00AA003F0F07}", 0, 0
'stdole
References.AddFromGuid "{00020430-0000-0000-C000-000000000046}", 0, 0
'DAO
References.AddFromGuid "{4AC9E1DA-5BAD-4AC7-86E3-24F4CDCECA28}", 0, 0
'Excel
References.AddFromGuid "{00020813-0000-0000-C000-000000000046}", 0, 0
'Scripting
References.AddFromGuid "{420B2830-E718-11CF-893D-00A0C9054228}", 0, 0
'IWshRuntimeLibrary
References.AddFromGuid "{F935DC20-1CF0-11D0-ADB9-00C04FD58A0B}", 0, 0
'Office
References.AddFromGuid "{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}", 0, 0

ErrHandler:
Select Case Err.Number
Case 32813
Resume Next
Case Is <> 0
 MsgBox "Run-time error '" & Err & "' : " & vbNewLine & vbNewLine & Error(Err)
Case Else
End Select

End Sub
Sub AddReference()
错误转到错误处理程序时:
“VBA
References.AddFromGuid“{000204EF-0000-0000-C000-0000000000 46}”,0,0
“进入
References.AddFromGuid“{4AFFC9A0-5F99-101B-AF4E-00AA003F07}”,0,0
“斯多尔
References.AddFromGuid“{00020430-0000-0000-C000-0000000000 46}”,0,0
"道"
References.AddFromGuid“{4AC9E1DA-5BAD-4AC7-86E3-24F4CDCECA28}”,0,0
"出类拔萃",
References.AddFromGuid“{00020813-0000-0000-C000-0000000000 46}”,0,0
“脚本
References.AddFromGuid“{420B2830-E718-11CF-893D-00A0C9054228}”,0,0
“我的图书馆
References.AddFromGuid“{F935DC20-1CF0-11D0-ADB9-00C04FD58A0B}”,0,0
“办公室
References.AddFromGuid“{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}”,0,0
错误处理程序:
选择案例错误编号
案件32813
下一步继续
案例为0
MsgBox“运行时错误”&Err&“:”&vbNewLine&vbNewLine&error(Err)
其他情况
结束选择
端接头

您安装的最后一个版本将成为默认版本

如果希望2010成为默认版本,则必须卸载这两个版本,然后重新安装2013 1st和2010 last。
Iamhhp☻♥

使用后期绑定?例如,
Set rs=CreateObject(“ADODB.Recordset”)
这样您就不需要添加任何引用。请注意,您仍然不能在函数或过程之外使用任何Word/PowerPoint/其他对象或枚举,因为编译器将在
WORKBOOK\u OPEN
事件开始执行之前失败。因此,您不能创建公共Word对象,也不能将参数的类型定义为Word/PPT类型(例如,您不能执行类似于
Sub-CopyActiveChartToWord(FormatType为WdPasteDataType)
)。