Create a VB Script component
VB Script component architecture
MEGA APIs are natively COM and can be used by COM application. The VB Script language uses the Dispatch protocol to handle objects and access their methods. Therefore using VB Script language to access MEGA objects is completely standard. The following diagram illustrates the way the VB Script component interacts with MEGA APIs:
Creating and editing a VB Script component
VB Script code can be written and used:
• in an external file
• in a MEGA macro
• directly in the MEGA script editor.
You can use the script editor to write VB scripts using MEGA APIs, which enables you to execute operations on HOPEX repository content.
Creating a VB Script component
To create a VB Script component with the Script Editor:
1. From HOPEX menu bar, select Tools > Script Editor to launch the script editor.
The Script Editor dialog box opens.
2. From Script Editor menu bar, select File > Save As.
3. Select either:
- a Macro to save the script as a macro in the repository,
- a File to save the script as an external VB Script file.
4. Enter your VB Script code.
Creating a VB Script macro with the wizard
HOPEX includes a wizard that enables preparation of macro parameters and code according to the use imagine by the user. Code is initialized according to the MEGA concept implemented.
To use the macro creation wizard on an object of TaggedValue type:
1. From HOPEX toolbar, click
Explore 
.
2. From Explore window, create a new object of the TaggedValue MetaClass.
3. Display Empty Collections.
4. Right-click Implementation folder and select New.
The Creation of Macro wizard appears.
5. Select Create a (VB)Script Macro.
6. Click Next.
7. (Optional) Modify the Name of the macro.
8. (Optional) If the macro you have created can be reused for another TaggedValue, select the Reusable.
9. Click Finish.
The macro (e.g.: “MyTaggedValue.Implementation) is created and its code already initialized.
The macro (e.g.: “MyTaggedValue.Implementation) is stored in the Macros folder, Unclassified Macros sub-folder.
Editing a VB Script code
The Script Editor menu bar enables you access commands that ease your editing actions.
The Script Editor tool bar enables you to easily access these commands to simplify your editing work.
Alternatively, you can use the menu shortcuts.
To edit a VB Script file/macro with the Script Editor:
1. In the Script Editor menu bar, select File > Open and select the script type a File or a Macro.
2. Select the file/macro.
The file/macro code is displayed in the Script Editor.
To execute the script:
In the
Script Editor toolbar, click
Execute 
.
Alternatively in the Script Editor menu bar, select Execute > Execute.
Global code execution starts.
To stop your script execution, in the
Script Editor toolbar, click
Stop 
.
To save the script code:
In the
Script Editor toolbar, click
Save 
.
Alternatively in the Script Editor menu bar, select File > Save As.
To move parts of the code to another position:
In the Script Editor toolbar, select the code part and click either:
Cut 
to cut the selected code
Copy 
to copy the selected code
Paste 
to paste the selected code
Alternatively in the Script Editor menu bar, select the File menu and then select the corresponding command or use the menu shortcuts:
To cancel or repeat an action you have carried out:
In the Script Editor toolbar, click:
Cancel 
to cancel your last action
Repeat 
to repeat you last action
Alternatively in the Script Editor menu bar, select the Edit menu and then select File menu and then select the corresponding command or use the menu shortcuts:
To enlarge or reduce font size:
In the Script Editor toolbar, click:
Enlarge Font 
to enlarge the font size
Reduce Font 
to reduce the font size
Alternatively in the Script Editor menu bar, select the View menu and then select the corresponding command.
To query or replace part of your code:
In the Script Editor toolbar, click:
Query 
to find a character string in your code
Replace
to find a character string in your code and replace it by another one
Alternatively, in the Script Editor menu bar, select Edit > Query or Edit > Replace.
To test your script:
In the
Script Editor toolbar, click
Execute 
to test your script.
If there is an error in your script, the line in error is highlighted and an error message is displayed in the bottom pane. This error message includes the line number in error and the error type.
To stop your script execution, in the
Script Editor toolbar, click
Stop 
.
Alternatively, in the Script Editor menu bar, select Execute > Execute or Execute > Stop.
To reach a specific point in your code:
1. In the Script Editor menu bar Select Edit > Go to Line.
2. In the dialog box that appears, enter the line number you want to reach and click OK.
Debugging a VB Script component
Activating macro debugging
To activate macro debugging:
1. From HOPEX menu bar, select Tools > Options.
2. In the left pane, expand Workstation and select Local Options.
3. In the right pane select Activation of macro debugging.
This option state is automatically written in the MEGAWKS.INI file (C:\ProgramData\MEGA\HOPEX <Version>\Cfg):
[API Script]
ScriptDebug=1
4. Exit HOPEX for the activation to be taken into account.
Macro Debugging
To debug a macro:
1. Right-click the macro to be debugged and select Edit.
The selected macro is edited in the Macro - Script Editor.
2. In the
Macro - Script Editor tool bar, click
Activate Debugging Stop Points 
.
3. Either:
• execute the macro: in the
Macro - Script Editor tool bar, click
Execute 
, or
• perform the sequence of events which calls the macro.
In both cases, the Mega Script Debugger opens:
4. Add breakpoints:
In the Mega Script Debugger, with the Breakpoints buttons you can:
• create and delete (Set/Remove) a breakpoint, or
• enable and disable (Enable/Disable) a breakpoint.
5. To create a breakpoint, put the cursor at the chosen code line and click Set/Remove: the breakpoint is created. It looks like a red sphere.
You can:
• disable a breakpoint: put the cursor at an already created breakpoint line and click Enable/Disable: the breakpoint is disabled.
It looks like a red circle.
• remove or enable a breakpoint: put the cursor at the chosen breakpoint line and click the Set/Remove button.
Click Enable All, Disable All, or Remove All when you want to apply the same command to all macro breakpoints.
Note: Commands on breakpoints are active whenever the Mega Script Debugger is opened.
6. Click Continue.
7. Do the sequence of events which allows executing the code where breakpoints are created and enabled.
The macro Script Debugger opens again,
A blue arrow in the breakpoint red sphere shows at which breakpoint the debugger has stopped.
• The three buttons Step Over, Step Into and Step Out allow running the program step by step, entering or going out of procedures: Functions and Subroutines.
A blue arrow always shows the current line.
• You can examine the current state of the program and track the variable values.
Write the variable name or expression to be evaluated at the bottom of the Mega Script Debugger window, and click Evaluate.
The value is displayed under the expression to be evaluated.
• You can evaluate complex expressions using API:
The result of:
"Name of 1st object in " & oMasterPlan.GetCollection("~7shQ6GeE8H50[Outside-the-Master-Plan milestone]").count & ": " & oMasterPlan.GetCollection("~7shQ6GeE8H50[Outside-the-Master-Plan milestone]").item(1).name
Is:
"Name of 1st object in 2: Auto Repair Service Improvement – Breakdown::As-Is"
• Another example of API use: the method Explore can be called on a MegaObject, and the Explorer is opened in another window:
1. In the Evaluation field enter: mgobjMegaObject.Explore.
2. Click Continue to continue the program execution.
When there is an error during the program execution, the Mega Script Debugger window opens, the program is stopped at the wrong line and an error message is displayed:
3. Click Continue to continue the program execution or click Close to stop it.
• To deactivate debugging for a macro when the Mega Script Debugger is opened, unselect Script break. It may be useful if the macro is called several times during a sequence of events and if the Debugger window opens every time it is called.
• If several macros have to be debugged at the same time, Debugging must be activated for each of them. The Mega Script Debugger window which corresponds to each of them is displayed when the corresponding macro code is executed to allow creating breakpoints.
Finding Text command bar
At the top of the Mega Script Debugger window, a specific command bar allows searching for a string in the macro script.
To find a string in the macro script:
In the Find field, enter the string and click Find Text.