Hi everyone,
I've just started learning to use VBA to import and organise data from HYSYS and would be grateful if someone could advise me on this.
Whilst I'm able to import values like MassFlow, Temperature and Pressure in the correct units using the GetValue method, I can't seem to do the same for ComponentMassFlow.
Option Explicit
Public hyApp As HYSYS.Application
Public simCase As SimulationCase
Option Compare Text
Public Sub StartHYSYS()
Dim fileName As String
Dim i As Integer
Dim FeedGas As ProcessStream
Dim Com As Component
Dim List As Components
' LOADING HYSYS SIMULATION FILE
Set hyApp = CreateObject("HYSYS.Application")
hyApp.Visible = True
Set simCase = hyApp.ActiveDocument
If simCase Is Nothing Then
fileName = Worksheets("Import").Range("B1").Text
Set simCase = GetObject(fileName, "HYSYS.SimulationCase")
simCase.Visible = True
End If
'Clear sheet
Worksheets("Import").Range("A2:AZ100").Clearcontents
'Connect Feed stream
Set FeedGas = simCase.Flowsheet.MaterialStreams.Item("Feed gas")
Worksheets("Import").Range("B3").Value = FeedGas.Name
'Load Components
Set List = simCase.Flowsheet.FluidPackage.Components
i = 0
For Each Com In List
i = i + 1
Worksheets("Import").Range("A" & i + 3).Value = Com.Name
Worksheets("Import").Range("B" & i + 3).Value = FeedGas.ComponentMassFlow(i - 1).GetValues("kg/h")
Next Com
'Total Stream Mass Flow
Worksheets("Import").Range("b49").Value = FeedGas.MassFlow.GetValue("kg/h")
End Sub
Doing this gives me the Run-time error '424' (Object required)
I've tried to get around this by Setting another variable to the individual components in ComponentMassFlow first, but it appears this isn't recognised as an array?
*The Method for ComponentMassFlow is "GetValues" rather than "GetValue" as it is for MassFlow - according to the HYSYS type library
I'm guessing this has something to do with the fact that ComponentMassFlow is a RealFlexVariable whilst MassFlow is a RealVariable.
Without the GetValues method, it currently returns the whole list of component mass flows in kg/s.
If I remove "(i-1)" from after ComponentMassFlow, it does appear to return the first component in kg/h.
Could someone please advise me as to how I might program it to return the list of component mass flows in a specified unit?
*If anyone else is in my position and is looking for resources to get them started (in addition to reading the "HYSYS Customization Guide"):
http://www.easyexcelvba.com
https://www.cheresou...xport-to-excel/
https://www.cheresou...hysys-to-excel/
https://www.cheresou...mto-hysysexcel/
http://hysyssimulations.blogspot.com/2011/06/how-to-link-hysys-simulation-to-excel.html