

are several possible causes for the following error message: One or more templates failed to merge or operation was cancelled:
WORD DOC MERGE SECOND PLACEMENT OF A FIELD IN SAME DOCUMENT HOW TO
The code sample below shows how to use the XML data source: If the data source contains multiple data tables, use the RichEditMailMergeOptions.DataMember property to define a specific data member. Use the RichEditMailMergeOptions.DataSource property to specify the data source used in a mail merge. RichEditDocumentServer supports the following data source types: ' Calculate the difference between the current date Private Shared Sub WordProcessor_CalculateDocumentVariable(ByVal sender As Object, ByVal e As CalculateDocumentVariableEventArgs)ĭim hireDate As DateTimeOffset = Convert.ToDateTime(e.Arguments(0).Value)ĭim currentDate As DateTimeOffset = DateTime.Now Var dif = currentDate.Subtract(hireDate) Calculate the difference between the current date Private static void WordProcessor_CalculateDocumentVariable(object sender, CalculateDocumentVariableEventArgs e)ĭateTimeOffset hireDate = Convert.ToDateTime(e.Arguments.Value) ĭateTimeOffset currentDate = DateTime.Now The nested MERGEFIELD refers to the HireDate entry in the database. In this example, this field should insert a number of years the employee worked at a company. The code sample below demonstrates how to handle the event to calculate the DOCVARIABLE field value. Variables can be stored in a document or calculated within the RichEditDocumentServer.CalculateDocumentVariable event. The DOCVARIABLE field allows you to insert any type of content into a document – from a simple variable to dynamic data from a database or another document. Use DOCVARIABLE Fields in the Mail Merge Template If Not Integer.TryParse(strId, id) Then Return Nothingĭim bytes As Byte() = Tr圜ast(Convert.FromBase64String(Tr圜ast(row(columnName), String)), Byte())ĭim memoryStream As MemoryStream = New MemoryStream(bytes) If Not uri.StartsWith(prefix) Then Return Nothingĭim strId As String = uri.Substring(prefix.Length).Trim() Public Function GetStream(ByVal uri As String) As Stream Public Sub New(ByVal sourceTable As DataTable, ByVal imageColumn As String) Shared ReadOnly prefix As String = "dbimg://" MemoryStream memoryStream = new MemoryStream(bytes) Convert the image string from this rowīyte bytes = Convert.FromBase64String(row as string) as byte Check if the string contains the primary key String strId = uri.Substring(prefix.Length).Trim() Remove the prefix from the retrieved URI string Public ImageStreamProvider(DataTable sourceTable, string imageColumn) Static readonly string prefix = "dbimg://" Public class ImageStreamProvider : IUriStreamProvider

The GetStream method parses the received URI (the INCLUDEPICTURE field) and finds the required data row, and returns the MemoryStream with an image. The INCLUDEPICTURE field in the template has a nested MERGEFIELD that refers to the EmployeeID field from the database. The code sample below shows the IUriStreamProvider implementation used to insert images from a database. IUriStreamProvider Service Implementation ' Create a field at the placeholder positionĭ(namePosition, "MERGEFIELD ""FirstName""") ' Find a placeholder for the FirstName field:ĭim nameRanges() As DocumentRange = document.FindAll("FirstName", SearchOptions.WholeWord)ĭim namePosition As DocumentPosition = nameRange.End ' Create a field at the placeholder's positionį(imagePosition, "INCLUDEPICTURE ""DevExpress-Logo-Large-Color.png""") Use the INCLUDEPICTURE field as follows: ", SearchOptions.WholeWord, pictureField.CodeRange).First()ĭ(nestedFieldRange.Start, "MERGEFIELD EmployeeID")ĭim footer As SubDocument = document.Sections(0).BeginUpdateFooter()ĭim imageRanges() As DocumentRange = footer.FindAll("image", SearchOptions.WholeWord)ĭim imagePosition As DocumentPosition = imageRange.End DOCVARIABLE Inserts formatted content: variables, document elements, or entire document content. Use one of the following fields to insert data from the database: MERGEFIELD Inserts plain text. The FieldCollection.Create method allows you to add fields to the template. WordProcessor.LoadDocument("template.docx")ĪddMailMergeFields(wordProcessor.Document) Using wordProcessor = New RichEditDocumentServer() WordProcessor.LoadDocument("template.docx") ĪddMailMergeFields(wordProcessor.Document) Using (var wordProcessor = new RichEditDocumentServer())
