1

I am trying to automatically add a reference while running a sub. But I get "User defined-type not defined" for project As VBAProject What's wrong?

  Sub tester()

    Dim SolverPath As String
    SolverPath = Application.LibraryPath & "\SOLVER\SOLVER.XLA"

    Dim wb As Workbook
    Set wb = ThisWorkbook

    Dim project As VBAProject
    project = wb.VBProject

    wb.project.References.AddFromFile SolverPath

End Sub
Ihidan
  • 558
  • 1
  • 7
  • 25
  • See here. http://stackoverflow.com/questions/9879825/how-to-add-a-reference-programmatically-in-excel-vba – MatthewD Aug 05 '15 at 16:39

1 Answers1

0

VBAProject is actually a special type of library, not a variable type. If you hit F2 to bring up the Object Browser, you can select it from the library dropdown. So you can't create variables of type VBAProject. However, it's not necessary to, nor for ThisWorkbook, unless you just want a shorter alias. Just use the workbook's VBProject property to gain access to the current project instance and add/remove components. Your code could be simplified to:

Sub tester()

    Dim SolverPath As String
    SolverPath = Application.LibraryPath & "\SOLVER\SOLVER.XLA"

    ThisWorkbook.VBProject.References.AddFromFile SolverPath

End Sub
Bond
  • 16,071
  • 6
  • 30
  • 53