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

时间:2015-04-20 13:19:58

标签: c# excel vba excel-vba

我通过c#代码将一个宏添加到Excel文件中以运行存储过程并执行它但是因为它没有引用ActiveX Data Objects 2.5库,它会出错:

  

" complie错误:用户定义的类型不是defiend"

当我手动添加该引用时,它可以正常但我想通过代码添加它,因为用户无法引用它。

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

1 个答案:

答案 0 :(得分:2)

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

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

这需要引用Microsoft ActiveX Data Objects X.X Library才能运行。但是,通过声明您的对象:

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

您可以避免必须添加引用的问题。有一个很好的讨论和整个子程序的例子,它是早期绑定的,而不是后期绑定的here

或者,您可以通过VBA本身添加引用。在此示例中,您可以使用C#打开工作簿,然后调用将检查相应引用的宏(如果缺少则添加它)。以下代码取自here

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

您需要更改的唯一内容是strGUID变量。您可以使用下面的这个小表来获取您想要使用的任何版本的相应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} ║
╚═════════════════════════╩════════════════════════════════════════╝

为了找到这些GUID值,我使用了here中的代码。

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