VBA Example to take control (Open/Run/Close/Reinit) of an Aspen Plus simulation via ActiveX automation

Download as pdf : 
Products: Aspen Plus 
Last Updated: 03-Nov-2020
Versions: 
Article ID: 000083571
Primary Subject: 
Converted from "107014_Default.txt"

Creation Date: 26-Dec-2001 09:04AM


Applicable Version(s):

10.2 and above


Problem Statement:

VBA Example to take control (Open/Run/Close/Reinit) of an Aspen Plus simulation via ActiveX automation.


Solution:

The attached Excel file contains a Visual Basic for Application (VBA) code that will:

  • Open any Aspen Plus simulation.
  • Run the simulation.
  • Reinitialize the simulation.
  • Close the simulation.

This example could be used as a starting point for building a VBA interface to retrieve specific Aspen Plus results.

To run this example, copy the included Excel file onto your hard-drive and open it. This Excel VBA code is setup to work with Aspen Plus 10.2. If you want to use it with other versions of Aspen Plus, proceed as follows:

  • Open the Excel file
  • Go to the Tools menu. Select Macro -> Macros -> Visual Basic Editor
  • Once in the VBA editor, go to the Tools menu and select reference
  • Select the library corresponding to the Aspen Plus version you have last installed on your PC (e.g. Aspen Plus GUI 1x.x Type Library, where 1x.x is the version number). If you can not find it, browse to the happ.tlb file (for Aspen Plus 10.x, it is by default located under \Program Files\AspenTech\Aspen Plus 10.2\GUI\xeq. For Aspen Plus 11.1, it is in \Program Files\AspenTech\APRSYSTEM 11.1\GUI\xeq).


Code Description

The VBA code contains a global variable that will hold the reference to the Aspen Plus ActiveX object: go_Simulation. This object is declared as an HappLS object.

  1. To open the simulation:
        Set go_Simulation = Getobject(lbFileName)

        where lvFileName contains the full path to the simulation file to be opened.

The Graphical User Interface (GUI) is then made visible or not, depending on the value of the chk_IsVisble checkbox. The code to make the GUI visible/invisible, depending on the checkbox state, is:

        go_Simulation.Visible = chk_IsVisible.Value


  1. To run the simulation, the Run2 method of the Engine property of the HappLS object go_Simulation is called, as follows:
        go_Simulation.Engine.Run2

The Run2 method has an optional argument (boolean type) that will make the run running synchronously or asynchronously (see Tech Tip # 102859 and # 104805 for more details).

  1. To reinitialize the simulation, the Reinit method for the Engine property of the HappLS object is used in subroutine cmd_Init_Click:
        go_Simulation.Engine.Reinit(IAP_REINIT_SIMULATION)

The argument passed to the Reinit method defines what part of the simulation should be reinitialized. Other valid arguments are:

IAP_REINIT_BLOCK
to reinitialize a block
IAP_REINIT_CONVERGENCE
to reinitialize a convergence block
IAP_REINIT_SIMULATION
to reinitialize the entire simulation
IAP_REINIT_STREAM
to reinitialize a stream


Technical tip 102365 provides more details about the Reinit method.

  1. To close the simulation, the GUI must be made non-visible and the HappLS object should be set to nothing:
        go_Simulation.Visible = False
        Set go_Simulation = Nothing

             See cmd_CloseSimulation_Click subroutine.

See Chapter 38 of the Aspen Plus User Guide for further details about the Aspen Plus ActiveX Automation Server.


KeyWords

ActiveX,COM,Automation,Visual Basic for Applications (VBA), Reinit


Attachments
Filename Date Added
RunControl.zip 18-Jan-2017