The While activity enables you to execute a specific process repeatedly, while a specific condition is met. The main difference between this and the Do While activity is that, in the first one, the condition is evaluated before the body of the loop is executed.
This type of activity can be useful to step through all the elements of an array, or execute a particular activity multiple times. You can increment counters to browse through array indices or step through a list of items.
Example of Using a While Activity
To exemplify how to use a While activity, let’s create an automation that increments an integer variable from 10 to 100, and writes all the numbers to a Microsoft Word document.
- Create a new sequence.
- Create an integer variable,
Counter
, with the default value of 10. - Add a While activity to Designer panel.
- In the Condition field, type
Counter
< 100. This means that the body of the loop is going to be repeated until the value of theCounter
variable is going to be bigger than 100. - In the Body section of the While activity, add an Assign activity.
- In the Properties panel, in the To field add the
Counter
. - In the Value field, type
Counter
+ 1. This increments the value of theCounter
with one. - Add an Append Text activity under the Assign one.
Note:
This activity is part of the Word activities package. If you do not have it, use the package manager functionality to install it.
- In the FilePath field, type the path of a Word document in between quotation marks.
Note:
Make sure that the Word document is not used when running the automation, otherwise a message error is displayed and the execution is stopped.
- In the Text field, type "The counter is now " +
Counter.ToString
+".".
The final project should look as in the following screenshot.
- Press F5. The automation is executed.
- Double-click the Word document specified at step 9. Note that all the numbers between 10 and 100 are written, as expected.
Updated 3 years ago
See Also
About Control Flow |