Share via

How do I remove a reference in VBA?

Anonymous
2014-11-12T13:53:20+00:00

Microsoft Access 2007

I can add a reference by using,

Application.VBE.ActiveVBProject.References.AddFromFile "C:\filelocation"

However

Application.VBE.ActiveVBProject.References.Remove "C:\filelocation"

Gets the Error of:

"Comile Error"

"Type Mismatch"

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

4 answers

Sort by: Most helpful
  1. Andreas Killer 144.1K Reputation points Volunteer Moderator
    2014-11-12T16:35:38+00:00

    Try this one, works everywhere, except you have no access to the VBA project.

    Andreas.

    Sub Test()

      Dim Ref As Object 'VBIDE.Reference

      With Application.VBE.ActiveVBProject

        For Each Ref In .References

          'With Ref

          '  Debug.Print .Name, .Major & "." & .Minor, .FullPath

          'End With

          If InStr(1, Ref.FullPath, "C:\FileLocation", vbTextCompare) = 1 Then

            .References.Remove Ref

            Exit For

          End If

        Next

      End With

    End Sub

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2014-11-12T16:43:25+00:00

    Figured it out Hans. I used:

    Dim  ref As References

    For Each ref in References

         If LCase (red.FullPath) = LCase("C:\FileLocation") Then

              References.Remove ref

              Exit For

         End If

    Next Ref   

    Thank you!

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2014-11-12T15:53:24+00:00

    Thank hans, but no go on the code.  Is what you provided for excel? 

    I tried replacing the "ActiveWorkbook" for "Application.VBE.Active" and on both attempts it would error on line

    "If LCase(ref.FullPath)..."

    I am guessing I am still having troubles locating the references in Visual Basic.

    Was this answer helpful?

    0 comments No comments
  4. HansV 462.6K Reputation points
    2014-11-12T14:52:35+00:00

    Remove expects a Reference object as argument, not a string. Try this:

        Dim ref As Reference

        For Each ref In ActiveWorkbook.VBProject.References

            If LCase(ref.FullPath) = LCase("C:\FileLocation") Then

                ActiveWorkbook.VBProject.References.Remove ref

                Exit For

            End If

        Next ref

    Was this answer helpful?

    0 comments No comments