Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Using Solver with VB6 Excel Object

Hello

I am trying to use Solver through an Excel object in VB6. I'm not sure how
to access Solver through an object objExcel after I've declared objExcel as
Excel.Application. Can anyone provide the syntax for doing this?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 146
Default Using Solver with VB6 Excel Object

Hi Jason -

If you open Excel through automation (CreateObject), no addins are started in that
instance. You need to start any addins through code.

Solver should be located within objExcel.ApplicationPath, which returns something
like this: C:\Program Files\Microsoft Office\Office\LIBRARY. It's in the
subdirectory Solver:

C:\Program Files\Microsoft Office\Office\LIBRARY\Solver\Solver.xla

You don't need to open this as an add-in, just use objExcel.Workbooks.Open to open it.

The official way to run Solver through automation is to set a reference to it and
run its VBA commands, but that path is littered with the bones of unsuccessful VBA
programmers, different versions of Excel and Solver having fought for the marrow.

The more reliable way to use solver is to use Excel's Application.Run command, with
the Solver command in quotes. The first thing you must do is run Solver's Auto_Open
procedu

objExcel.Run "solver.xla!SOLVER.Solver2.Auto_open"

This makes sure that Solver is properly initialized. (Having used Workbooks.Open to
open Solver may have made this step redundant, but I haven't tested this in VB.)

The rest of Solver is automated much the same way. Record some macros in Excel to
get most of the way there, then convert to App.Run syntax. You'll have to make sure
not to skip arguments because the error messages are not very enlightening.

Here is a sampling of commands, roughly in order of how you might invoke them. Many
of the items in quotes are named cells in Excel; you could use the qualified range
addresses instead.

' Reset Solver
objExcel.Run "solver.xla!SolverReset"

' Add Constraints
objExcel.Run "solver.xla!SolverAdd", "Agg1", 3, 0
objExcel.Run "solver.xla!SolverAdd", "Vol1", 2, "Target1"

' Press the "OK" Button
objExcel.Run "Solver.xla!SolverOK", "VolumeMixTotal", 2, _
"TargetVolume", "Cell1,Cell2,Cell3"

' Get the solution
' Result is returned to indicate the relative success of the analysis
Result = objExcel.Run("solver.xla!SolvSolve", True)
If Result <= 3 Then
' Result = 0, Solution found, optimality and constraints satisfied
' Result = 1, Converged, constraints satisfied
' Result = 2, Cannot improve, constraints satisfied
' Result = 3, Stopped at maximum iterations
MsgBox "Solution Found", vbInformation
Else
' Result = 4, Solver did not converge
' Result = 5, No feasible solution
Beep
MsgBox NoSolution, vbExclamation
End If

You'll have to play around quite a bit to make sure it works, record several sets of
models to get the parameters and syntax, and I've left out some of the error traps
and so forth.

This approach worked on a wide variety of combinations of machine, Windows version,
and Office version. The approach with references being set would work on one
machine, then not on any others. It would be fixed for another machine, then not
work on the original one. My partner and I pulled out a lot of hair until we came
across Application.Run.

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Peltier Technical Services
Tutorials and Custom Solutions
http://PeltierTech.com/
_______

jason77 wrote:

Hello

I am trying to use Solver through an Excel object in VB6. I'm not sure how
to access Solver through an object objExcel after I've declared objExcel as
Excel.Application. Can anyone provide the syntax for doing this?


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 146
Default Using Solver with VB6 Excel Object

Jason -

If you're using Excel only to get at Solver, you might want to check out Frontline
Systems' other Solver products, which can be used outside of Excel, i.e., directly
from VB. It's likely to be more straightforward to use, especially since you'd be
distributing it, not relying on what random installations your users may have.

http://www.solver.com/

(I have no vested interest in making this suggestion, and I've never used anything
beyond the standard Excel Solver.)

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Peltier Technical Services
Tutorials and Custom Solutions
http://PeltierTech.com/
_______

Jon Peltier wrote:

Hi Jason -

If you open Excel through automation (CreateObject), no addins are
started in that instance. You need to start any addins through code.

Solver should be located within objExcel.ApplicationPath, which returns
something like this: C:\Program Files\Microsoft Office\Office\LIBRARY.
It's in the subdirectory Solver:

C:\Program Files\Microsoft Office\Office\LIBRARY\Solver\Solver.xla

You don't need to open this as an add-in, just use
objExcel.Workbooks.Open to open it.

The official way to run Solver through automation is to set a reference
to it and run its VBA commands, but that path is littered with the bones
of unsuccessful VBA programmers, different versions of Excel and Solver
having fought for the marrow.

The more reliable way to use solver is to use Excel's Application.Run
command, with the Solver command in quotes. The first thing you must do
is run Solver's Auto_Open procedu

objExcel.Run "solver.xla!SOLVER.Solver2.Auto_open"

This makes sure that Solver is properly initialized. (Having used
Workbooks.Open to open Solver may have made this step redundant, but I
haven't tested this in VB.)

The rest of Solver is automated much the same way. Record some macros in
Excel to get most of the way there, then convert to App.Run syntax.
You'll have to make sure not to skip arguments because the error
messages are not very enlightening.

Here is a sampling of commands, roughly in order of how you might invoke
them. Many of the items in quotes are named cells in Excel; you could
use the qualified range addresses instead.

' Reset Solver
objExcel.Run "solver.xla!SolverReset"

' Add Constraints
objExcel.Run "solver.xla!SolverAdd", "Agg1", 3, 0
objExcel.Run "solver.xla!SolverAdd", "Vol1", 2, "Target1"

' Press the "OK" Button
objExcel.Run "Solver.xla!SolverOK", "VolumeMixTotal", 2, _
"TargetVolume", "Cell1,Cell2,Cell3"

' Get the solution
' Result is returned to indicate the relative success of the analysis
Result = objExcel.Run("solver.xla!SolvSolve", True)
If Result <= 3 Then
' Result = 0, Solution found, optimality and constraints satisfied
' Result = 1, Converged, constraints satisfied
' Result = 2, Cannot improve, constraints satisfied
' Result = 3, Stopped at maximum iterations
MsgBox "Solution Found", vbInformation
Else
' Result = 4, Solver did not converge
' Result = 5, No feasible solution
Beep
MsgBox NoSolution, vbExclamation
End If

You'll have to play around quite a bit to make sure it works, record
several sets of models to get the parameters and syntax, and I've left
out some of the error traps and so forth.

This approach worked on a wide variety of combinations of machine,
Windows version, and Office version. The approach with references being
set would work on one machine, then not on any others. It would be fixed
for another machine, then not work on the original one. My partner and I
pulled out a lot of hair until we came across Application.Run.

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Peltier Technical Services
Tutorials and Custom Solutions
http://PeltierTech.com/
_______

jason77 wrote:

Hello

I am trying to use Solver through an Excel object in VB6. I'm not
sure how to access Solver through an object objExcel after I've
declared objExcel as Excel.Application. Can anyone provide the syntax
for doing this?




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
2 Label Options - Forms Object vs Control Box Object Awrex Excel Discussion (Misc queries) 3 July 17th 09 07:10 PM
Interesting Solver problem (Solver encounters an error) MrShorty Excel Discussion (Misc queries) 3 December 22nd 05 10:52 PM
How to check Solver and Forms 2.0 Object Library permanently? yoyo2000 Excel Discussion (Misc queries) 1 September 26th 05 03:04 AM
Resetting Solver Manually to Fix Solver Bug Stratuser Excel Programming 0 September 13th 04 07:04 PM
Range object to Array object conversion Myrna Larson[_2_] Excel Programming 1 August 1st 03 02:27 AM


All times are GMT +1. The time now is 12:12 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"