通过c#自动添加对Excel VBA的引用

我通过c#代码添加一个macros到一个Excel文件运行存储过程,并执行它,但因为它没有参考ActiveX数据对象2.5库,它会得到错误:

“complie错误:用户定义的types不defiend”

当我手动添加引用时,它会好的,但我想通过代码添加它,因为用户不能引用它。

然后,我需要以编程方式检查用户PC,看是否存在对Microsoft ActiveX Data Objects 2.5 Library或更高版本的引用,如果不存在,请使用我的macros中的C#代码或VBA代码创build它。

可能最简单的解决方法是在VBAmacros中使用晚期绑定。 例如,如果在我的子程序中,我声明:

 Dim cn As ADODB.Connection Dim rs As ADODB.Recordset 

这需要引用Microsoft ActiveX数据对象XX库才能运行。 但是,通过声明你的对象是这样的:

 Dim cn As Object, rs As object Set cn = CreateObject("ADODB.Connection") Set rs = CreateObject("ADODB.Recordset") 

您可以避免添加引用的问题。 有一个体面的讨论和一个完整的子程序的例子,这是一个早期的绑定在这里的后期。

或者,您可以通过VBA本身添加引用。 在这个例子中,你可以用C#打开工作簿,然后调用一个macros来检查相应的引用(如果缺失,就添加它)。 下面的代码是从这里取的。

 Sub AddReference() 'Macro purpose: To add a reference to the project using the GUID for the 'reference library Dim strGUID As String, theRef As Variant, i As Long 'Update the GUID you need below. strGUID = "{B691E011-1797-432E-907A-4D8C69339129}" 'Set to continue in case of error On Error Resume Next 'Remove any missing references For i = ThisWorkbook.VBProject.References.Count To 1 Step -1 Set theRef = ThisWorkbook.VBProject.References.Item(i) If theRef.isbroken = True Then ThisWorkbook.VBProject.References.Remove theRef End If Next i 'Clear any errors so that error trapping for GUID additions can be evaluated Err.Clear 'Add the reference ThisWorkbook.VBProject.References.AddFromGuid _ GUID:=strGUID, Major:=1, Minor:=0 'If an error was encountered, inform the user Select Case Err.Number Case Is = 32813 'Reference already in use. No action necessary Case Is = vbNullString 'Reference added without issue Case Else 'An unknown error was encountered, so alert the user MsgBox "A problem was encountered trying to" & vbNewLine _ & "add or remove a reference in this file" & vbNewLine & "Please check the " _ & "references in your VBA project!", vbCritical + vbOKOnly, "Error!" End Select On Error GoTo 0 End Sub 

唯一需要改变的是strGUIDvariables。 您可以使用下面的这个小表来获取适合您想要使用的任何版本的strGUID。 您可能还想删除消息框部分,这取决于您使用Excel究竟在做什么。

 ╔═════════════════════════╦════════════════════════════════════════╗ ║ Microsoft ADODB Version ║ GUID ║ ╠═════════════════════════╬════════════════════════════════════════╣ ║ 2.5 ║ {00000205-0000-0010-8000-00AA006D2EA4} ║ ║ 2.6 ║ {00000206-0000-0010-8000-00AA006D2EA4} ║ ║ 2.7 ║ {EF53050B-882E-4776-B643-EDA472E8E3F2} ║ ║ 2.8 ║ {2A75196C-D9EB-4129-B803-931327F72D5C} ║ ║ 6.1 ║ {B691E011-1797-432E-907A-4D8C69339129} ║ ╚═════════════════════════╩════════════════════════════════════════╝ 

为了find这些GUID值,我使用了下面的代码。

 Sub ListReferencePaths() 'Macro purpose: To determine full path and Globally Unique Identifier (GUID) 'to each referenced library. Select the reference in the Tools\References 'window, then run this code to get the information on the reference's library On Error Resume Next Dim i As Long With ThisWorkbook.Sheets(1) .Cells.Clear .Range("A1") = "Reference name" .Range("B1") = "Full path to reference" .Range("C1") = "Reference GUID" End With For i = 1 To ThisWorkbook.VBProject.References.Count With ThisWorkbook.VBProject.References(i) ThisWorkbook.Sheets(1).Range("A65536").End(xlUp).Offset(1, 0) = .Name ThisWorkbook.Sheets(1).Range("A65536").End(xlUp).Offset(0, 1) = .FullPath ThisWorkbook.Sheets(1).Range("A65536").End(xlUp).Offset(0, 2) = .GUID End With Next i On Error GoTo 0 End Sub