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
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

Norie

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

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,730
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. 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,130,129
Messages
5,640,286
Members
417,135
Latest member
zeusmining

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top