Private Sub InjectSolverMacro(ByRef xlbook As Excel.Workbook, ByRef xlapp As Excel.Application)
Dim xlVBProj As VBProject
Dim xlModule As VBIDE.VBComponent
Dim sCode As String
xlbook.Worksheets(1).Select
AddIns("Solver Add-In").Installed = True
Set xlVBProj = xlbook.VBProject
xlVBProj.References.AddFromFile ("C:\Program Files\Microsoft Office\Office14\Library\SOLVER\Solver32.dll")
Set xlModule = xlbook.VBProject.VBComponents.Add(vbext_ct_StdModule)
sCode = "Private sub SolverMacro()" & vbCr _
& "SolverAdd CellRef:=""$D$6"", Relation:=1, FormulaText:=""1"" " & vbCr _
& "SolverAdd CellRef:=""$D$6"", Relation:=3, FormulaText:=""0"" " & vbCr _
& "SolverOk SetCell:=""$F$6"", MaxMinVal:=2, ValueOf:=0, ByChange:=""$B$6:$D$6"", Engine:=1, EngineDesc:=""GRG Nonlinear"" " & vbCr _
& "SolverSolve" & vbCr _
& "End Sub"
xlModule.CodeModule.AddFromString (sCode)
End Sub