3.1.4 Linking Excel and HYSYS - 1 PDF
3.1.4 Linking Excel and HYSYS - 1 PDF
3.1.4 Linking Excel and HYSYS - 1 PDF
Introduction
One of the most common uses of the Automation capabilities in HYSYS
is linking an Excel spreadsheet to the HYSYS program. This allows the
user to run a simulation from the Excel environment, utilizing Excel’s
presentation and mathematical capabilities.
All of the power of HYSYS can be accessed while the complexity of the
program is hidden from the user.
Learning Objectives
By completing this module, you will learn:
Prerequisites
Before beginning this module, you should be:
2
Linking Excel and HYSYS 3
The VB language allows for early binding. This means that the object
variables are declared not as generic objects, but as specific objects.
Early binding allows the compiler to associate the object variable with
its proper type at design time. The object variable declaration will look
like this:
At first glance, it may seem that early binding of the object variables will
add to the complexity of the program and increase the possibility of
errors. However, early binding the object variables should actually
reduce the errors in the program and increase the program’s operating
speed. Because of these advantages, early binding will be used in all
future programs developed in this course.
The terms early and late
binding only apply to object Early binding of the object variables in VB allows the program to use the
variables. Intellisense tool developed by Microsoft. This tool will reduce the
amount of typing needed when developing a program, and should
decrease the number of syntax errors in the program.
Early and late binding can be used together in the same program. It is a
good idea to use early binding for objects that will be used quite often
3
4 Linking Excel and HYSYS
in a program. Using late binding for an object that will be used only
infrequently will not significantly decrease the program’s speed.
This naming scheme is completely for the benefit of the code developer
and anyone who might be trying to read and understand the code.
Using these simple names does not help VB to associate the object with
its proper type.
If, however, the following line of code is added to the program, an error
will occur when the program is compiled.
The error will occur because the declared object type (SimulationCase)
cannot hold a reference to a stream object.
Choosing the right object type to match the object variable will become
much easier as you gain experience with the HYSYS Type Library. For
now, a few examples of the most commonly used object variables will
4
Linking Excel and HYSYS 5
be given here to help match the right object type to the object variable.
If the correct object type is not known, it is better to declare the object
generically (use late binding) rather than risk an error when the
program is compiled. Late binding must also be used whenever the
object type will not be known until the macro is run.
5
6 Linking Excel and HYSYS
Exercise
The following are several generic object variable declarations. Change
these declarations to specific object variable declarations. Assume, for
the purposes of this exercise, that the object variable’s name represents
the reference that the object will hold. For example, hyCase will hold a
reference to the simulation case, nothing else.
Solution
1. Dim hyCase As ________________
2. Dim hyFlowsheet As ________________
3. Dim hyStream As ________________
4. Dim hyApp As ________________
5. Dim hyOperation As ________________
6. Dim hyFluidPkg As ________________
7. Dim hyBlends As ________________
8. Dim hySpreadsheetCell As ________________
6
Linking Excel and HYSYS 7
Application and SimulationCase are special objects in that they are the
two possible entry points for HYSYS Automation. In other words, these
objects represent doors that must be opened before other objects in
HYSYS can be accessed. The object hierarchy diagram given previously
illustrated this graphically.
The following two tables outline the various command forms, what the
function does and whether the function will reference the Application
or SimulationCase.
7
8 Linking Excel and HYSYS
If HYSYS is Running:
Reference
Function Syntax Result
Returned
8
Linking Excel and HYSYS 9
Reference
Function Syntax Result
Returned
Write out one of the commands that will start HYSYS, open
a specified case and create a reference to that case.
_____________________________________________________
This is the command structure that will be used most often
in this course.
9
10 Linking Excel and HYSYS
All of the commands given above must be used with the Set keyword. If
the command returns a reference to the Application, the syntax of the
command will be similar to:
Set hyCase = _
hyApp.SimulationCases.Open("c:\hysys\mycase.hsc")
This command will open the specified case through the hyApp object.
It must be placed after the line that contains the linking function, i.e.
the Set hyApp = ... line.
10
Linking Excel and HYSYS 11
Conversely, you may want to access the Application object, but your
linking function has referenced the SimulationCase object. There is a
simple way to do this; use the command syntax as given below:
This command will allow you to access the Application object from the
SimulationCase object.
There are other ways of linking Excel and HYSYS (the New keyword and
the As New statement); however, they will not be used here. More
information about the methods presented here can be obtained from
the VB on-line help.
Exercise
In this exercise, you are asked to provide two lines of code that will
complete a VB macro. When complete, this macro will open a
hypothetical HYSYS case named "knock-out.hsc" located on the root
directory of the C: drive, and display the temperature of the first stream
in that case. The macro is partially completed as follows:
11
12 Linking Excel and HYSYS
In order to used the various commands outlined above, you must know
the name of the HYSYS case and its exact location in the directory
before you can open it.
Also, specifying the name and location of the HYSYS case in the
program code will work well if the VB program is meant to run only
with the specified HYSYS case. Using the program with another HYSYS
case will require the programmer to return to the code base and rewrite
the filename. If it is likely that the program will be used with several
different HYSYS cases, a better solution must be found.
That better solution uses the Open File Dialog. This feature is available
through Excel and allows the user to find and specify the HYSYS case
that the program will use.
This feature is called by replacing the Dim hyCase ... and Set hyCase =
... lines with the following eight lines of code.
12
Linking Excel and HYSYS 13
The user can now navigate the directory structure and determine which
HYSYS file they want to open.
Could the Set hyCase = ... line in the code on the previous
page be rewritten as
Set hyCase = GetObject (filename, "HYSYS.Application")?
_________________
Why or why not?
_____________________________________________________
What about rewriting it as:
Set hyCase = GetObject (filename, _
"HYSYS.SimulationCase")? _________________
13
14 Linking Excel and HYSYS
14