In an upcoming release of ReportMagic, we have added support for macros in Word headers and footers. This is currently slated for version 3.22, which should be released by March or April.
Here’s a very simple example showing a [ForEach:] macro that outputs three numbers, located inside the Word header, and a [String:] macro in the body which creates the variable used by the [ForEach:].
When processed by ReportMagic, the output document appears like this:
You can do the equivalent using macros in footers. Not only that, since Word supports multiple headers and footers (e.g. per section), you can of course put different macros into each one. A simple use case might be to create a template document that should use a different company logo in the header based upon some condition. In this case, for each logo you could simply create a shape in Word with the correct position, size, and image fill, then use the [Shape.Delete:] macro to delete whichever ones are not needed.
Macro Processing Order
The order of macro processing is strictly as follows:
- Macros in the entire main document are processed FIRST
- Macros in headers are processed SECOND
- Macros in footers are processed LAST
This means that can create variables in the main document and access them later in the headers/footers, as in my example, which creates the variable Numbers initially.
Limitations
Note that the order that macros are processed in the header or footer is generally the order that they were created in each header / footer, and generally the order that the headers/footers were created, however this cannot be guaranteed, due to the way that Word structures its XML.
Therefore, you should try to ensure that macros in headers/footers try to be as self-contained as possible. For example, you can even now include macros in shapes in Word (another topic on this will follow shortly), but you can’t guarantee that the macros in such a shape will be executed before text macros.
In terms of which macros you can use in headers and footers, there aren’t strictly any limitations. You can use text-based macros, shapes, loops such as [ForEach:], table macros, and much more. There’s certain to be a few gotchas that we find in the coming months, but if you encounter any please let us know.