title: How to programmatically add .xlam addin (as object) to vba?
body: <p>Hi I'm trying add a reference programnatically as per: <a href="https://stackoverflow.com/questions/594239/how-to-add-remove-reference-programmatically/594475#comment70317603_594475">How to Add/Remove reference programmatically?</a></p>
  
  <p>However, the reference I wish to programmatically add is the .xlam file of opensolver.org. </p>
  
  <p>So with the following code:</p>
  
  <pre><code>    Sub AddWS()
    'Create a reference to Windows Script Host, '
    'where you will find FileSystemObject '
    'Reference name: "IWshRuntimeLibrary" '
    'Reference Name in references list: "Windows Script Host Object Model" '
    ReferenceFromFile "C:\Users\path\OpenSolver2.8.5_LinearWin\OpenSolver.xlam"
    End Sub
  </code></pre>
  
  <p>and function:</p>
  
  <pre><code>Function ReferenceFromFile(strFileName As String) As Boolean
  Dim ref As Object
  MsgBox (strFileName)
  
           'On Error GoTo Error_ReferenceFromFile
           'References.AddFromFile (ref)
           'ref = strFileName
           'vbProj.References.AddFromFile strFileName
           References.AddFromFile (strFileName)
           MsgBox (strFileName)
           ReferenceFromFile = True
  
  Exit_ReferenceFromFile:
           Exit Function
  
  Error_ReferenceFromFile:
           ReferenceFromFile = False
           Resume Exit_ReferenceFromFile
  End Function
  </code></pre>
  
  <p>But I get object error 424: "Object required" on the line: </p>
  
  <pre><code>         References.AddFromFile (strFileName)
  </code></pre>
  
  <p>It is because I do not know how to either:</p>
  
  <p>a. Set the .xlam file as object
  b. convert the .xlam file to an .ocx file so that it will be read and added to reference.</p>
  
  <p>Does anyone have any suggestions on how to automatically programmatically add the OpenSolver.xlam to microsoft Excel from any random location?*</p>
  
  <p>*For me it works fine if I paste the folder containing the .xlam in:</p>
  
  <pre><code>C:\Program Files (x86)\Microsoft Office\root\Office16\Library
  </code></pre>
  
  <p>and then open it manually, but I need the users of my code to also be able to run it from a mac without requiring them to manually add the opensolver as a reference before I call it from another module.</p>
  
  <p>As mentioned below the succesfull code indeed adds the <code>OpenSolver.xlam</code> to the references, but I cannot seem to programmatically Mark the checkbox in vba>tools>references.</p>
  
  <p><strong>Does anyone have any suggestions on the code that automatically marks that checkbox before <code>Opensolver</code> in the references, once it is already added?</strong> </p>