0

I'd like to call OPL CPLEX and solve problems in a program coded on Excel VBA. I want to create an OPL model and, load .mod and .dat file into that model. After a long search process on the internet, I came up with some lines of script below released on IBM website.

Sub CPLEXCallHere()

'To create the Concert environment
Dim oplF As Oplfactory

'To create the error handler
Dim errorHandler As OplErrorHandler

'To identify the model source
Dim modelSource As OplModelSource

'To identify the model definition
Dim def As OplModelDefinition

'To create the engine instance
Dim cp As cp

'To create the OPL model
Dim opl As OplModel

'Specifying a data source
Dim dataSource As OplDataSource

oplF = New Oplfactory   'concert environment
errorHandler = oplF.CreateOplErrorHandler() 'error handler

modelSource = oplF.CreateOplModelSource(DATADIR + "/SA_MP_R1.mod") 'model source
def = oplF.CreateOplModelDefinition(modelSource, settings) 'model definition
cp = oplF.CreateCP()    'engine instance

opl = oplF.CreateOplModel(def, CPLEX) 'OPL model

dataSource = oplF.CreateOplDataSource(DATADIR + "/1 SA_MP_R1_1.dat")     'specifying data source
opl.AddDataSource (dataSource)

'Generating the Concert model
opl.Generate

'Solving the model
If (CPLEX.Solve()) Then

End If

'Accessing the solution through OPL
 opl.PrintSolution (Console.Out)
End Sub

However, to be able to use the script above, there should be a function to be declared the OPL library or oplall.dll file as follows:

Public Declare Function AccessCPLEX Lib "C:\ILOG\CPLEX_Studio1261\opl\lib\oplall.dll" Alias "oplall" [([arglist])] [As type]

I do not have enough experience for a declaration function like that so I am very confused. What would the type of the function (application, object, long, string etc.) be? How can I introduce variable types including Oplfactory, OplModelSource, OplModelDefinition, etc. into the VBA?

Any help would be a lot appreciated.

Thanks,

Community
  • 1
  • 1
Yusuf
  • 31
  • 1
  • 7

1 Answers1

-2

you may have a look at

https://github.com/AlexFleischerParis/howtowithopl/blob/master/oil.mod

There you will find an example on how to simply call oplrun from VBA

The key part is to call external exe oplrun.exe

Private Sub Button1CallOPL_Click()
MsgBox "External call to OPL"

Dim strPath As String
strPath = "C:\ILOG\CPLEX_Studio127\opl\bin\x64_win64\oplrun.exe C:\poc\callOPLfromExcel\oil.mod C:\poc\callOPLfromExcel\excelbuttonoilSheet.dat"

Shell strPath

End Sub

regards

Alex Fleischer
  • 9,276
  • 2
  • 12
  • 15
  • Alex Fleischer this code works for me but if I change the location of the mod and/or dat file it breaks. Any idea why? – tjnel Jun 19 '20 at 19:22