Hysys Excel Automation Training

Friday, May 3, 2013

How to create and acces molar component (molar flowrate, composition, ect)

It has been so long time not to update this blog. And now I'm posting how to access stream component fraction, component molar flowrate and mass flowrate.

Here is the VBA example




Option Explicit
Public hyApp As HYSYS.Application
Public simCase As SimulationCase
Public pStream As ProcessStream
Public Sub StartHYSYS()
Dim filename As String
Dim ITEM As Integer
Dim Compositions As Variant        

' HYSYS CUSTOMATION THROUGH EXCEL TO ACCESS STREAM COMPONENTS        


' LOADING HYSYS SIMULATION FILE    


Set hyApp = CreateObject("HYSYS.Application")        

hyApp.Visible = True    
Set simCase = hyApp.ActiveDocument    
If simCase Is Nothing Then        
          filename = Worksheets("HYSYS").Range("C4").Text               
If filename <> "False" And simCase Is Nothing Then            
          Set simCase = GetObject(filename, "HYSYS.SimulationCase")            
          simCase.Visible = True        
End If    
End If      


' READ DATA FROM EXCEL TO "STREAM 1"    

Set pStream = simCase.Flowsheet.MaterialStreams.ITEM("STREAM 1")    
Compositions = pStream.ComponentMolarFractionValue    
For ITEM = 0 To 11        'collect composition data as variant        
          Compositions(ITEM) = Worksheets("HYSYS").Range("D" & ITEM + 7).Value    
Next ITEM   

' apply composition data    

pStream.ComponentMolarFraction.Values = Compositions            
' WRITE COMPOSITION DATA FROM HYSYS TO EXCEL FROM "STREAM 3"       

Set pStream = simCase.Flowsheet.MaterialStreams.ITEM("STREAM 3")    

For ITEM = 0 To 11        ' number of components are 12
  ' component molar fraction        
Worksheets("HYSYS").Range("E" & ITEM + 7).Value = pStream.ComponentMolarFraction(ITEM)
  ' component mass flowrate        
Worksheets("HYSYS").Range("F" & ITEM + 7).Value = pStream.ComponentMassFlow(ITEM)
   ' component molar fraction    
Worksheets("HYSYS").Range("G" & ITEM + 7).Value = pStream.ComponentMolarFlow(ITEM)
Next ITEM            

' FINISH


End Sub



Spreadsheet is look like this figure


Please note, hysys file source is located in Cell C4, refer to following code 
               filename = Worksheets("HYSYS").Range("C4").Text

Hysys stream is configured as following figure





Hope that helps. Any thought? Please feel free to comment.
, , ,

7 comments:

  1. Awesome post mate,

    Just checking - do you need a "Next ITEM"

    Before the "Finish" line?

    Cheers,

    Tristan

    ReplyDelete
  2. Tristan,

    "Next item" should be in the next line. And yes, we need this command.

    ReplyDelete
  3. Hi, I would like to ask you if it's possible to change some equipment specs (example: outlet temperature or reboiler duty of a tower)from excel?? Can you make a post with an example? Thanks a lot, your blog is very helpful.

    ReplyDelete
  4. Hi Jorge,


    Yes, it is posible to change equipment spec. I'll show you how to change it. And for the example is column operation. Please wait, I'm preparing it.

    Many thank for visiting my blog.

    ReplyDelete
  5. Hi,
    this link s examples are very useful. I am trying to get my code to run correctly but I am having problem with the propriety method. I am trying to activate some of the functions in preferences on VBA but still not working.
    I am getting error 438
    Can you please help?
    Regards
    Mayembe



    ReplyDelete
  6. Hi Mayembe,

    I've opened your code and your hysys file. unfortunately i couldn't open your hysys file. It seems that you're using latest version of aspen hysys.

    Thus, i assumed that you are generating hysys file as same as mine.


    I checked your VBA code, and I also faces the same problem. "Object does not support this proprity or method." I clicked debug, and I see that this following code is not working.

    "Set pStream = simCase.Flowsheet.MaterialStream.ITEM("STREAM1")"

    This may you're facing also, I assume. If it is, your code is not correctly defined, since there is no stream "STREAM1" on hysys file, but "STREAM 1". So, stream name doesn't exist on your hysys file. That's why you have that error message.

    Hope that helps.

    Alex

    ReplyDelete
  7. Great post, I have found it really helpful.

    May I ask, how do you select the input for a hysys simulation from a row of data?
    In your example the data is in columns and that works on my simulation but due to the vast amount of data I am working with I need to display them in rows.

    Would be nice to hear back from you.

    Many Thanks

    Reggie

    ReplyDelete