Here is a snippet of the Word template…
{ MERGEFIELD TableStart:Level1 }
{ IF { MERGEFIELD VList } = "Yes" "{ MERGEFIELD TableStart:Level2 } {MERGEFIELD TextF3 \!format "html:TextF3" \* MERGEFORMAT } { MERGEFIELD TableEnd:Level2 }" "No records to report" } }
{ MERGEFIELD TableEnd:Level1 }
{ MERGEFIELD TableStart:Level1 }
{ IF { MERGEFIELD VList } = "Yes" "{ MERGEFIELD TableStart:Level2 } {MERGEFIELD TextF3 \!format "html:TextF3" \* MERGEFORMAT } { MERGEFIELD TableEnd:Level2 }" "No records to report" } }
{ MERGEFIELD TableEnd:Level1 }
VList is a field in the Level1 table. TextF3 is a field in the Level2 table. Level2 is a child of Level1.
Level1 contains three rows. In the first two rows the VList value is “No”. In the third row the value is “Yes”.
What I am noticing is that, since the IF condition is false in the first two rows, the merge fields in the true condition stay in the document. I need to find a way to get rid of the merge fields after each row in Level1 is processed. This is necessary because we are intercepting the merge field processing (by implementing IMergeFieldCallback) so that we manipulate the value before storing it in the document. We use the MoveToMergeField method to replace the merge field with the manipulated value. I put a break point in our code and the code only gets executed for the third row. Because the merge fields are not being removed by the time we get to the third row there are three MERGEFIELDs in the document with a field of TextF3, so when I call the MoveToMergeField(“TextF3”), it goes to the wrong one (it goes to the first row when I want the value to be placed in the third row). I need to find a way to get rid of the MERGEFIELD when the IF condition is false.
How can I have the Mail Merge process eliminate the IF condition after every row.
I do have the CleanupOptions set to remove Contained Fields (.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveEmptyParagraphs | MailMergeCleanupOptions.RemoveUnusedRegions | MailMergeCleanupOptions.RemoveUnusedFields | MailMergeCleanupOptions.RemoveContainingFields;)
but that does not seem to take care of the problem.