1

I add a macro by c# code to an Excel file for running stored procedure and execute it but because it has not reference of ActiveX Data Objects 2.5 library, It gets error:

"complie error : user-defined type not defiend "

When I add that reference manually it will be OK but I want to add it via code because user can't reference it.

Then I need programmatically to check the users PC to see if a reference to Microsoft ActiveX Data Objects 2.5 Library or later exists and if it does not, create it by C# code or VBA code in my macro.

1
  • You could use late binding instead? Commented Apr 20, 2015 at 13:28

2 Answers 2

4

Probably the easiest solution is to use late binding in your VBA macro. For example, if in my subroutine I am declaring:

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

This requires the reference to the Microsoft ActiveX Data Objects X.X Library in order to run. However, by declaring your objects as such:

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

You can likely avoid the issues with having to add the reference. There is a decent discussion and example of an entire subroutine that is early bound vs. late bound here.

Alternatively, you can add references through VBA itself. In this example you could open the Workbook with C#, then call a macro that will check for the appropriate reference (and add it if it is missing). The code below is taken from 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

The only thing that you will need to change is the strGUID variable. You can use this little table below to get the appropriate strGUID for whichever version you want to use. You may want to remove the message box portion too, depending on what exactly you are doing with 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} ║
╚═════════════════════════╩════════════════════════════════════════╝

And to find these GUID values, I used the code below from 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
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your reply , where do i add above code ? because the first line of my code(Sub Button1_Click()) gets error of "complie error : user-defined type not defiend" and when i add refrence manualy it will be ok , is it any mistake in my code? in body of my code i call stored procedure
thanks a lot . my error solved , i declare my variable use late bound , it's ok
sorry now i have an error "argument are of the wrong type are out of acceptable range" in this line cmd.Parameters.Append cmd.CreateParameter("ShipperID", adInteger, adParamInput, 1, 1) and when i add refrence of Microsoft ActiveX Data Objects 2.5 Library it will be ok
0

You can use Workbook.VBProject.References.AddFromFile (or AddFromGuid) from C# too.

https://learn.microsoft.com/en-us/office/vba/api/access.references.addfromfile

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.