While working on a word template, there was a need to put repeat blocks in an IF field, similar to this:
WordTemplate sample code using .DOC/.DOCX:
{ IF {MERGEFIELD DataSource1.FieldName}=”123456” “IF SECTION
Then a repeat block here using a table row that is bookmarked:
------------------------------------------------------------
| «Datasource1.IdColHeader» | «Datasource1.NameColHeader» |
------------------------------------------------------------
|«RepeatingDatasource2.Id» | «RepeatingDatasource2.Name» |
------------------------------------------------------------
” “Else Section
Second set of repeat block with different datasource
-------------------------------------------------------
| Id | Name |
-------------------------------------------------------
|«RepeatingDatasource3.Id»|«RepeatingDatasource3.Name»|
-------------------------------------------------------
”}
C# sample code using WordWriter's WordTemplate:
private static void ProcessWordTemplate(WordTemplate docTemp)
{
try
{
docTemp.Process();
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
}
For a DOCX WordTemplate, the error occurs when docTemp.Process()
is called. I get the following exception:
Exception Type: System.Application
Exception Message: WordWriter Error: Bookmarks cannot be rewritten
For a DOC WordTemplate, the documents was rendered but it only shows one row in the repeat block even if the actual result is more than 1.
But, if it is the other way around, an IF field in a repeat block, the IF field and the repeat block where successfully evaluated. The resulting document shows all the records.
Please note that I'm using OfficeWriter's WordWriter Version 9.