Almost there


#1

However in the following code, builder is not declared. Where should I declare it?

Private Sub HandleMergeField(ByVal sender As Object, ByVal e As Reporting.MergeFieldEventArgs)

If (e.DocumentFieldName.StartsWith("tea")) Then

builder.MoveToMergeField(e.DocumentFieldName)

builder.InsertHtml(e.FieldValue)

End If

End Sub


#2

Here is a full code that I used:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Cursor.Current = Cursors.WaitCursor

Dim filename As String = Application.StartupPath + "\\Doc1.doc"

Dim doc As Document = New Document(filename)

builder = New DocumentBuilder(doc)

AddHandler doc.MailMerge.MergeField, AddressOf HandleMergeField

' --- the following code is for MailMerge without database

' --- substitute it with your code

Dim sr As StreamReader = File.OpenText(Application.StartupPath + "\InsertHtml.txt")

Dim htmltext As String = sr.ReadToEnd()

sr.Close()

Dim fields(1) As String

fields(0) = "h1"

fields(1) = "h2"

Dim values(1) As String

values(0) = htmltext

values(1) = htmltext

doc.MailMerge.Execute(fields, values)

' --- end of MailMerge process

doc.Save(System.IO.Path.GetFileNameWithoutExtension(filename) + "_modified.doc")

Cursor.Current = Cursors.Default

End Sub

Private builder As DocumentBuilder

Private Sub HandleMergeField(ByVal sender As Object, ByVal e As Aspose.Word.Reporting.MergeFieldEventArgs)

If (e.DocumentFieldName.StartsWith("h")) Then

builder.MoveToMergeField(e.DocumentFieldName)

builder.InsertHtml(e.FieldValue)

End If

End Sub

Test files in attachment