1. Home
  2. Administrators
  3. Document templates
  4. Macros
  5. ‘For each’ macros in Word 365 templates

‘For each’ macros in Word 365 templates

This macro for Docuo templates in Word allows you to display the data of one or more “child” documents of another category, by means of text fragments that are repeated once for each child document.

‘For each’ macros in Word templates are only available in Docuo Enterprise.

For example, if we have two categories: “Classes” and “Students” related to each other through a drop-down list, we can create a Word 365 template of a class that shows as many of the same paragraphs as students belonging to the class, each one with its data.

The macro definition is done in the Word document itself with a syntax like:

@DOCUO.FOREACH{Data source, [order], [filter], [single replacement], [top]}

Insert here a Word text with labels inside as a template that will be repeated for each document found in the data source

@DOCUO.ENDFOREACH{}

Where:

Data source: Supports a field label or the code of a category. If we use a field label we must specify the label of the drop-down list field in the “child” category (students in the example) that connects it to the “parent” category (classes in the example). If we specify the code of a category we will be able to get any document from it, but in this case it is convenient to specify a filter that limits the results (see below).

Order: Optional parameter. Field labels by which you want to sort in ascending (ASC) or descending (DESC) order. Supports multiple fields separated by commas. If not specified, it is sorted by code

Filter: Optional parameter. Allows you to specify with T-SQL syntax the data filtering conditions.

Single replacement: Optional parameter. If TRUE, the table data is replaced only once. Default is FALSE so that each time labels are replaced the data is automatically updated.

Top: Optional parameter. Default is 100, it indicates the maximum number of records to show.

In the case of students in a school class, an example would be (within the Word template in the Class category):

Class: <@Properties:Code>
Course: <@Class data:Course>

@DOCUO.FOREACH{<@STUDENTS:Data:Class>, '<@STUDENTS:Data:Name> ASC'}

The student: “<@STUDENTS:Data:Name>”, born this year: <@STUDENTS:Data:Year of birth> and enrolled in the year <@STUDENTS: Data: Enrollment year>.

@DOCUO.ENDFOREACH{}

The content of the For Each macro will be repeated once for each “child” document (class students) up to a limit of 100 repetitions.

For Each macros can be nested. For example, in the “Students” category we have another child category of “Authorized Adults” we could also show the information of the authorized adults for each student:

Class: <@Properties:Code>
Course: <@Class data:Course>

@DOCUO.FOREACH{<@STUDENTS:Data:Class>, '<@STUDENTS:Data:Name> ASC'}

Student: <@STUDENTS:Data:Name>, born this year: <@STUDENTS:Data:Year of birth> and enrolled in the year <@STUDENTS: Data: Enrollment year>.

Authorized adults who can pick it up at school are:

@DOCUO.FOREACH{<@AUTHORIZED:Data:Student>, '<@AUTHORIZED:Data:Name> ASC'}

Mr/Mrs <@AUTHORIZED:Data:Name> with ID number <@AUTHORIZED:Data:ID>.

@DOCUO.ENDFOREACH{}

@DOCUO.ENDFOREACH{}

It is also possible to nest table -type macros within a ‘For each’ macro. Following the same example above, we could display the list of adults in a table:

Class: <@Properties:Code>
Course: <@Class data:Course>

@DOCUO.FOREACH{<@STUDENTS:Data:Class> , '<@STUDENTS:Data:Name> ASC'}

Student: <@STUDENTS:Data:Name>, born this year: <@STUDENTS:Data:Year of birth> and enrolled in the year <@STUDENTS: Data: Enrollment year>.

Authorized adults who can pick it up at school are:

@DOCUO.TABLE{<@AUTHORIZED:Data:Student>, '<@AUTHORIZED:Data:Name> ASC'}

Name ID
<@AUTHORIZED:Data:Name> <@AUTHORIZED:Data:DNI>

@DOCUO.ENDTABLE{}

@DOCUO.FOREACH{}

Updated on 19 de April de 2022
Was this article helpful?

Related Articles