Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
Thanks for your request. Here is code example how you can perform mail merge with regions and use MergeField event handler. Also see attached template.
Dim mCurrentName As String
Sub <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />Main()
Dim lic As Aspose.Words.License = New Aspose.Words.License()
lic.SetLicense("Aspose.Words.lic")
Dim table As New DataTable("MyTable")
table.Columns.Add("Name")
table.Columns.Add("Option1")
table.Columns.Add("Option2")
table.Columns.Add("Option3")
table.Columns.Add("Option4")
table.Columns.Add("Option5")
Dim noRep As Integer = 0
For i As Integer = 0 To 4
noRep = i
table.Rows.Add(New Object() {"Alexey" & i.ToString(), "Option1", "Option2", "Option3", "Option4", "Option5"})
Next
'Open template document
Dim doc As New Document("in.doc")
'Add MergeField event handler
AddHandler doc.MailMerge.MergeField, AddressOf HandleMergeField
'Execute mail merge with regions
doc.MailMerge.ExecuteWithRegions(table)
'Save output document
doc.Save("out.doc")
End Sub
Sub HandleMergeField(ByVal sender As Object, ByVal e As Aspose.Words.Reporting.MergeFieldEventArgs)
'Here you can place your conditional logic
If (e.FieldName = "Name") Then
mCurrentName = e.FieldValue
Else
If (e.FieldName = "Option1" And mCurrentName = "Alexey0") Then
e.Field.Remove() 'dont show Option1 if name is Alexey0
End If
End If
End Sub
Also see the following links.
http://www.aspose.com/documentation/file-format-components/aspose.words-for-.net-and-java/mail-merge-with-regions-explained.html
http://www.aspose.com/documentation/file-format-components/aspose.words-for-.net-and-java/execute-mail-merge-with-regions.html
Best regards.