Desktop User Guides > Professional > Getting started with UNICOM Intelligence Professional > Creating a macro to insert common code
 
Creating a macro to insert common code
Next. you create a macro that you can use to insert some common code into the script.
1 Click Tools > Macros.
The AutoExpansion Macros dialog opens.
This graphic is described in the surrounding text.
2 Click the mrScriptBasic tab.
3 In Macro Name, enter a name for the macro. For example, FE.
4 In Macro Text box, type the following:
For Each {0} In {1}

Next
The items in braces, {0} and {1}, define parameters. When you subsequently use the macro, UNICOM Intelligence Professional replaces these with the first and second arguments that you supply.
5 Click the Add Macro toolbar button.
This adds the macro to the list of mrScriptBasic macros.
6 Close the AutoExpansion Macros dialog.
Using the macro
Now you can use the macro in a script.
1 Back in the main Edit pane, type the following (where FE refers to the name you gave the macro. Use exactly the same combination of upper and lower case letters that you used when you defined its name.)
FE(MyVariableInstance,MDM.Variables)
2 Type Ctrl+M.
UNICOM Intelligence Professional inserts the macro and replaces the first parameter ({0}) with "MyVariableInstance" and the second parameter ({1}) with "MDM.Variables". (This is a simplistic example to demonstrate using the macro feature. In practice you would normally use macros to insert more complex code.)
This graphic is described in the surrounding text.
3 Use the ScriptAssist feature to insert code into the loop to write the full name of the variable instance to the text file and to add code after the loop to close the text file.
For Each MyVariableInstance In MDM.Variables
  MyTextFile.WriteLine(MyVariableInstance.FullName)
Next

MyTextFile.Close()
4 Run the code by doing one of these:
Press Ctrl+F5
Select the Start Without Debugging tool
Click Debugging > Start Without Debugging.
If you are using the Save Before Execution option, UNICOM Intelligence Professional asks for a name for the script file. If necessary, enter a name (for example, MyFirstScript.mrs). A message appears telling you that the script has completed successfully. If not, there might be an error in the script; to find it, see Debugging mrScriptBasic code.
5 If your script ran successfully, open the text file in UNICOM Intelligence Professional.
This graphic is described in the surrounding text.
See also
Getting started with UNICOM Intelligence Professional