We are attempting to use words mailmerge to insert text and an image into word document. The text is working properly, however trying to insert to a byte array into the field and by using the <> merge field tag is not. Our issue is that it gets replaced by what looks like the string representation of the byte array rather than the text (system.Byte).
There are several items that leads me to believe that the issue is that words is not recognizing the Image: prefix.
- The event handler is not being fired during the mail merge
- When I change the tag from <> to <>, I receive the same system.Byte string in place of an image.
The code that is producing the issue can be found below, however is just a small piece of the total code.
Private Function createSubmissionDocument(ByVal templateFields As Entities.templateItems, ByVal templateFile As String) As Document Dim doc As Document Dim dalDocProcessor As DAL.DocProcessor Dim docStream As FileStream Try 'load template file dalDocProcessor = New DAL.DocProcessor docStream = dalDocProcessor.GetDocumentStream(templateFile) 'load the stream into a aspose words class doc = New Document(docStream) If templateFields.Count > 0 Then AddHandler doc.MailMerge.MergeImageField, AddressOf HandleMergeImage 'Fill the fields in the document with user data. doc.MailMerge.Execute(templateFields.getTemplateKeys, templateFields.getTemplateValues) End If Catch ex As Exception Throw ex End Try Return doc End Function ' This is called when mail merge engine encounters Image:XXX merge field in the document. ' You have a chance to return an Image object, file name or a stream that contains the image. Private Sub HandleMergeImage(ByVal sender As Object, ByVal e As MergeImageFieldEventArgs) Dim imageStream As MemoryStream = New MemoryStream(CType(e.FieldValue, Byte())) ' Now the mail merge engine will retrieve the image from the stream. e.ImageStream = imageStream End Sub