If you want to embed the MS Word in your web page or form then do the mail merge, the Edraw Office Viewer OCX can save you lots of time.

Mail merge is a useful tool in scenarios where we want to randomly generate alike documents where just a few fields change.

Now for programmatically replacing the Mail Merge fields using the code, the document by default has many fields in it. But the user entered fields comes with a prefix and suffix which can be can be used as an identifier to replace the fields.

The following code can be added in the DocumentOpened event in the office viewer component.

Dim oMissing As Object = System.Reflection.Missing.Value()
Dim oTrue As Object = True
Dim oFalse As Object = False
Dim oWordDoc As New Word.Document()
Dim oTemplatePath As Object = "d:\Template.dot"
oWordDoc = EDOffice1.ActiveDocument()
For Each myMergeField As Word.Field In oWordDoc.Fields
iTotalFields += 1
Dim rngFieldCode As Word.Range = myMergeField.Code
Dim fieldText As String = rngFieldCode.Text
If fieldText.StartsWith(" MERGEFIELD") Then
Dim endMerge As Int32 = fieldText.IndexOf("\")
Dim fieldNameLength As Int32 = fieldText.Length - endMerge
Dim fieldName As String = fieldText.Substring(11, endMerge - 11)
fieldName = fieldName.Trim()
If fieldName = "MyField" Then
oWord.Selection.TypeText("This Text Replaces the Field in the Template")
End If
End If