Automatic add reference - Not working

nclacs99

Board Regular
Joined
Aug 23, 2012
Messages
54
Hello,

I have a workbook that adds an automatic reference to an external database (Microsoft ADODB Version 6.1).

The problem is that when I attempt to run the macro on another computer, it fails. Unfortunately, I don't get an error message and Excel crashes.

I noticed that if I delete the code below (that sets up the reference) and manually set up the external reference, the macro runs as intended. Any idea what is causing Excel to crash?

Code:
     '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
 

Some videos you may like

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,541
Office Version
365
Platform
Windows
Is the required library definitely available on the machine the code fails on?
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
34,802
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
It would, IMO, be far better to late bind the code.
 

nclacs99

Board Regular
Joined
Aug 23, 2012
Messages
54
Is the required library definitely available on the machine the code fails on?
Yes, the library is available. One of the ways I checked this is by deleting the code and manually selecting the library on the failing computer.
 

nclacs99

Board Regular
Joined
Aug 23, 2012
Messages
54
It would, IMO, be far better to late bind the code.
I tried late binding but didn't have any luck (my coding skill is pretty low). Can you provide code/format for code to conduct a late bind?
 

Watch MrExcel Video

Forum statistics

Threads
1,095,799
Messages
5,446,547
Members
405,406
Latest member
tuxy

This Week's Hot Topics

Top