Variables in header and footer of legacy Word reports

Fill variables in headers and footers of legacy Word templates via macro code

Product line

Standard

|

Expert

Operating mode

CLOUD ABO

|

ON-PREMISES

Modules

Services & CRM

Budget & Phases

Purchases

Resource Planning

Business Intelligence

Created: 19.10.2006
Updated: 05.06.2024 | Note that referencing variables by style is limited to Vertec-generated legacy reports.

In Word headers and footers, OCL cannot be evaluated because comments cannot be inserted. If you want to use values from Vertec, you can:

Insert data fields in headers and footers

This variant is supported starting with Vertec 6.1. See Legacy Word Reports for a description.

Insert variable in header or footer

In versions prior to Vertec 6.1 there is the following workaround via the macro code (see the Event Macros section in the article Legacy Word reports ):

As a variable, a text is inserted that can be uniquely retrieved later in the code. In the example below, the text is xProjectcode:

Macro code

The example would like to show the project code in the header or footer of an invoice. For this to work, a macro named Beforereport2 or Afterreport3 must be created on this invoice template. Macro with such name are automatically triggered when a report is called from within Vertec (see Event Macros section in the Legacy Word Reports article).

The code to replace the text in the header and footer for this example is as follows:

Sub AfterReport3(vertec As Object, dok As Object, root As Object, optarg As Object, akt As Object)
Dim i as integer
For i = 1 To dok.Sections.Count
  'Set the value in the header
   dok.Sections(i).Headers(wdHeaderFooterPrimary).Range.Find.Execute FindText:=”xProjectcode,” ReplaceWith:=root.Eval(“project.code”), Replace:=wdReplaceAll
'Set the value in the footer
  dok.Sections(i).Footers(wdHeaderFooterPrimary).Range.Find.Execute FindText:=”xProjectcode,” ReplaceWith:=root.Eval(“project.code”), Replace:=wdReplaceAll
Next
End Sub

The text you want to replace (xProject Code) must be typed directly in the header. If it is in a text box or table, the method described above will not find it.

Referencing Variables by Style

Another variant is to reference a specific piece of text within the report in the header or footer. This can be done as follows:

Initial situation: there is a master volume, and in it is a text project code. This text is marked with the comment code.

Select the text Project Code and then go to the styles. Select New Style. In the dialog box that appears, enter Project Code as the name, select symbols for the style type and click OK.

Then navigate to the header (or footer) where you want to show the project code. Type project code and highlight this text. Click Insert > Quick Blocks > Field. For categories, select Link and References, and for the field name, Styleref. Select Project code and click OK.

Nothing appears at first glance, but when you execute the document, the corresponding project code will be printed here.

Note:

This variant over StyleRef fields does not work with Vertec-generated Legacy Word reports with output format PDF.

To know if a field is a Styleref field, you can use the keyboard shortcut Shift-F9 to show the field code:

Insert line breaks in legacy Word reports via VBA

In legacy Word reports, a line break can be inserted via ASCII code chr(13) + chr(10):

txt1 = “First Row” & Chr(13) & Chr(10) & “Second Row”
ActiveDocument.Range.Find.Execute FindText:=”Text1,” ReplaceWith:= txt1, Replace:=wdReplaceAll

If the variable you want to replace is in a table, this will not work. In this case, the newline must be inserted via ^p:

txt2 = “First Row” & “^p” & “Second Row”
ActiveDocument.Range.Find.Execute FindText:=”Text2,” ReplaceWith:=txt2, Replace:=wdReplaceAll
Bitte wählen Sie Ihren Standort