Automation Example - Creating A Case From Scratch
Automation Example - Creating A Case From Scratch
Applicable Version(s)
All
Problem Statement
Automation example - Creating a case from scratch
Solution
The example code below illustrates this. To use the code: open up HYSYS, paste the code into the VBA
editor in Excel, set the code to reference the required case file name, make a reference to the HYSYS type
library (Tools ... References menu option in the VBA editor) and run the procedure. For troubleshooting
advice on common HYSYS / OLE Automation errors see Knowledgebase Solution #112361.
'Procedure-----------------------------------------------------------------
'Link to the currently open app
Set hyApp = CreateObject("HYSYS.Application") 'Links to open instance or starts a new one
hyApp.Visible = True
'First delete any existing case of the same name - if this isn't done then the .Add line below
'opens the existing case
strCase = "d:\Temp.hsc"
If Dir(strCase) <> "" Then Kill (strCase)
'Link to Basis
Set hyBasis = hyCase.BasisManager
'Since we started a new case we'll be in the basis environment anyway - could check for this a
If hyBasis.IsChangingBasis = False Then
'Turn off the solver - if we do this now it prevents HYSYS asking us if we want to
'be in Hold mode when we come out of the basis.
hyCase.Solver.CanSolve = False
hyBasis.StartBasisChange
End If
'Add a new fluidpackage - the parameter passed is the "Name" of the FP as it will
'appear in the Basis Manager
Set hyFluidPkg = hyBasis.FluidPackages.Add("New FP")
'Set the type of the fluid package
hyFluidPkg.PropertyPackageName = "PengRob"
'confusingly in some cases (e.g. PR) this is different to the type you see if you examine
'the PropertyPackageName property using the watch window
'All Property Package names can be found in Techtip #109180 in the Knowledgebase
'Switch off Solver - stops HYSYS trying to solve after every change is made
hyCase.Solver.CanSolve = False
End With
'Connect them up
hyCool.FeedStream = hyStrm
Set hyStrm = hyFlwSht.EnergyStreams.Add("Cooler Q")
hyCool.EnergyStream = hyStrm
Set hyStrm = hyFlwSht.MaterialStreams.Add("Cooler Out")
hyCool.ProductStream = hyStrm
hySep.Feeds.Add hyStrm
Set hyStrm = hyFlwSht.MaterialStreams.Add("Vapour")
hySep.VapourProduct = hyStrm
Set hyStrm = hyFlwSht.MaterialStreams.Add("Liquid")
hySep.LiquidProduct = hyStrm
Set hyStrm = hyFlwSht.MaterialStreams.Add("Water")
hySep.HeavyLiquidProduct = hyStrm
'Set parameters
hyCool.PressureDrop.SetValue 0.5, "bar"
hyCool.ProductTemperature.SetValue 10, "C"
'Switch on Solver
hyCase.Solver.CanSolve = True
End Sub
Note
The Knowledge Base examples are provided for academic purposes only and as such are not subject to the
quality and support procedures of officially released heritage-Hyprotech products. Users are strongly
encouraged to check performance and results carefully and, by downloading, agree to assume all risk related
to the use of these examples. We invite any feedback through the normal support channel at
[email protected].
KeyWords
Case, New