Generate a Word document with multiple parts

We want to create a Word document which consists of multiple parts. If the number of parts depends on the data read (e.g. customers, projects,...), we cannot work with a single rtf template.

An alternative to the solution described here is to generate the document via Word automation, but the required coding will then become more complicated.


  • Create a placeholder file "&[rtfinclude.###].rtf" which contains a small arbitrary text, for example "Template for sales office"
  • Create a master template file that contains a table with one cell and one column, no border.  As content of the table cell set a link to the placeholder file created in the first step. For the steps needed in Word see the example below.
  • In the InputScript, first generate all parts separately as .rtf files, using the technique described in the previous tip "Generate a Word document".
  • Use a suitable unique key field as part of the file name, for example a project number or a customer number.
  • In the InputScript generate the target rtf file using the master template. All linked documents are inserted by Word and we obtain a single document which includes all the parts that we have generated before.

We generate a Word document that shows all customer orders for one month, with separate parts for each sales office.


1. Placeholder file "&[rtfinclude.###].rtf&q"


2. Master template file

Start with a document that contains all fixed parts such as a header, footer, title, page count,...

Then insert a table with one row, one column and add a link to the placeholder file in the table cell. This is done as follows:


Click on Insert -> Object -> Text from file.

Then select the placeholder file and choose "Insert as Link":


The master template now looks as follows:

The default behaviour of Word is to load a document without updating the linked documents. A user can change this in his Word options, but we recommend you force the updating of the linked documents by adding the following macro to the master template

Save the file as "mastertemplate.rtf".


3. Sales office file

Next we create our template which we will use to generate the rtf files for each sales office. Here we can use single fields and tables as show in the preceding tip. For our example we use the following template "salesoffice.rtf":

When we run our InputScript (see below), we obtain the final result, a Word document which contains separate parts for each sales office:





We read the customer orders directly from the database tables VBAK and KNA1 via the function /GUIXT/dbselect.

// clear variables
Clear V[rtfinclude.*]

// read orders
Clear text[r]
Call "/guixt/dbselect" _
in.table="VBAK join KNA1 on KNA1~KUNNR = VBAK~KUNNR" _
in.orderby="VBAK~VKBUR,D1" _
in.condition="VBAK~AUDAT BETWEEN '20180201' AND '20180228' AND VBAK~TRVOG = '0' " _

// fill rtf variables
Set V[rtf.month] "February"
Set V[rtf.year] "2018"

Set V[k] 1   // returned value
Set V[n] 1   // table row
Set V[m] 1   // sales office

label next_value

// customer name
CopyText fromText="r" toString="rtf.cn.&V[n]" line=&V[k]
if Q[ok]
Set V[k] &V[k] + 1
// street
  CopyText fromText="r" toString="rtf.cs.&V[n]" line=&V[k]
Set V[k] &V[k] + 1

  // post code
  CopyText fromText="r" toString="rtf.cp.&V[n]" line=&V[k]
Set V[k] &V[k] + 1

  // city
  CopyText fromText="r" toString="rtf.cc.&V[n]" line=&V[k]
Set V[k] &V[k] + 1

  // order number
  CopyText fromText="r" toString="rtf.on.&V[n]" line=&V[k]
Set V[k] &V[k] + 1

  // order date
  CopyText fromText="r" toString="rtf.od.&V[n]" line=&V[k]
Set V[k] &V[k] + 1

  // order value
  CopyText fromText="r" toString="rtf.ov.&V[n]" line=&V[k]
Set V[rtf.ov.&V[n]] "&V[rtf.ov.&V[n]]" + 0 _
decimals=0 groupseparator=" "
  Set V[k] &V[k] + 1

   // sales office
  CopyText fromText="r" toString="office" line=&V[k]
  Set V[k] &V[k] + 1

  if V[rtf.office] and not V[rtf.office=&V[office]]
     // generate sales office rtf
     CopyText fromTemplate="C:\temp\salesoffice.rtf" toText="temp"
     Set V[rtfinclude.&V[m]] "sales.&V[rtf.office]"
     CopyText fromText="temp" toFile="C:\temp\sales.&V[rtf.office].rtf"
 Set V[m] &V[m] + 1
 Set V[n] 0


  // current sales office
  Set V[rtf.office] "&V[office]"

  Set V[n] &V[n] + 1
goto next_value


// generate last office
if V[rtf.office]

  // generate sales office rtf
  CopyText fromTemplate="C:\temp\salesoffice.rtf" toText="temp"
  Set V[rtfinclude.&V[m]] "sales.&V[rtf.office]"
  CopyText fromText="temp" toFile="C:\temp\sales.&V[rtf.office].rtf"


// generate final document using the master template
CopyText fromTemplate="C:\temp\mastertemplate.rtf" toText="temp"
CopyText fromText="temp" toFile="C:\temp\sales.doc"

// call up word