Hello all,
I have a workbook that I would like to distribute to others. The workbook uses the Solver add-in. To ensure all users have Solver installed and enabled on their versions of Excel I wanted to create a WorkbookOpen event that checks for it, and if it isn't installed installs it then enables the reference.
I have the following code. There is no way of telling if the installation portion works or not because I already have it on my machine. However it does not enable the Solver add-in in the References list. Could someone point me in the right direction on how to accomplish the installation and activation of Solver?
Private Sub Workbook_Open()
Call FindSolverexcel
End Sub
Option Explicit
Sub FindSolverexcel()
'Search the add-in file in your computer and then try to install
On Error Resume Next
With Application.FileSearch
.NewSearch
.SearchSubFolders = True
.FileName = "Solver.xla"
.LookIn = Application.Path
.Execute
If .Execute > 0 Then
If AddIns("Solver Add-in").Installed = False Then
AddIns("Solver Add-in").Installed = True
'if not installed check the cause
If Err.Number > 0 Then
'Cause is security restriction
If Err.Number = 1004 Then
MsgBox "Please check if your security setting do not allow you to use VBProject" _
& vbCr & "Click Tools, Macro, security" & vbCr _
& "trusted source check access to Visual Basic Project" & vbCr & "close and open the file again" _
& vbCr & "if there is no option then proceed"
Err.Clear
Exit Sub
Else
'cause is just not in the add-in list yet and it try to reinstall again
AddIns.Add(.FoundFiles(1)).Installed = True
Err.Clear
End If
End If
'gives the message that installation is successful
CreateObject("WScript.Shell").Popup "Solver successfully installed", 1, "Solver"
End If
Else
MsgBox "Solver Not Installed In this Computer", vbCritical
Exit Sub
End If
End With
Dim i As Integer, x As Long
'checks if the solver is already reference in the vba tool
x = ThisWorkbook.VBProject.References.Count
For i = 1 To x
If ThisWorkbook.VBProject.References(i).Name = "SOLVER" Then
CreateObject("WScript.Shell").Popup "Reference aleady set", 1, "Solver"
Exit Sub
End If
Next i
'this put the solver as reference in vba tool
ThisWorkbook.VBProject.References.AddFromFile Application.LibraryPath _
& Application.PathSeparator & "SOLVER" & Application.PathSeparator & "SOLVER.XLA"
CreateObject("WScript.Shell").Popup "Reference successfully installed", 1, "Solver"
'this just clear the solver bugs
Application.Run "Solver.xla!auto_open"
End Sub
Bookmarks