Hi PRA,
Refer the below sample code.
C#
using System;
using System.Data;
using Word = Microsoft.Office.Interop.Word;
public partial class CS : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[5] {
new DataColumn("LastName"), new DataColumn("FatherName"), new DataColumn("Adress"), new DataColumn("Name"), new DataColumn("Birthday") });
dt.Rows.Add("Pulodov", "Abdulloevich", "city Dushanbe", "Rustam", "22.12.1987");
if (dt.Rows.Count > 0)
{
object fileName = Server.MapPath("~/Files/Test.doc");
Word.Application word = new Word.Application();
Word.Document doc = new Word.Document();
object missing = System.Type.Missing;
try
{
doc = word.Documents.Open(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
doc.Activate();
foreach (Microsoft.Office.Interop.Word.Range docRange in doc.Words)
{
if (docRange.Text.Trim() == "#")
{
docRange.Text = docRange.Text.Replace("#", "");
}
else if (docRange.Text.Trim() == "Ln")
{
docRange.Text = docRange.Text.Replace("Ln", dt.Rows[0]["LastName"].ToString());
}
else if (docRange.Text.Trim() == "Fn")
{
docRange.Text = docRange.Text.Replace("Fn", dt.Rows[0]["FatherName"].ToString());
}
else if (docRange.Text.Trim() == "Ad")
{
docRange.Text = docRange.Text.Replace("Ad", dt.Rows[0]["Adress"].ToString());
}
else if (docRange.Text.Trim() == "Nm")
{
docRange.Text = docRange.Text.Replace("Nm", dt.Rows[0]["Name"].ToString());
}
else if (docRange.Text.Trim() == "Bth")
{
docRange.Text = docRange.Text.Replace("Bth", dt.Rows[0]["Birthday"].ToString());
}
}
doc.Save();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
doc.Close(ref missing, ref missing, ref missing);
((Word._Application)word).Quit();
}
}
}
}
VB.Net
Imports Word = Microsoft.Office.Interop.Word
Imports System.Data
Partial Class VB
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(4) {New DataColumn("LastName"), New DataColumn("FatherName"), New DataColumn("Adress"), New DataColumn("Name"), New DataColumn("Birthday")})
dt.Rows.Add("Pulodov", "Abdulloevich", "city Dushanbe", "Rustam", "22.12.1987")
If dt.Rows.Count > 0 Then
Dim fileName As Object = Server.MapPath("~/Files/Test.doc")
Dim word As New Word.Application()
Dim doc As New Word.Document()
Dim missing As Object = System.Type.Missing
Try
doc = word.Documents.Open(fileName, missing, missing, missing, missing, missing, _
missing, missing, missing, missing, missing, missing, missing, missing, missing, missing)
doc.Activate()
For Each docRange As Microsoft.Office.Interop.Word.Range In doc.Words
If docRange.Text.Trim() = "#" Then
docRange.Text = docRange.Text.Replace("#", "")
ElseIf docRange.Text.Trim() = "Ln" Then
docRange.Text = docRange.Text.Replace("Ln", dt.Rows(0)("LastName").ToString())
ElseIf docRange.Text.Trim() = "Fn" Then
docRange.Text = docRange.Text.Replace("Fn", dt.Rows(0)("FatherName").ToString())
ElseIf docRange.Text.Trim() = "Ad" Then
docRange.Text = docRange.Text.Replace("Ad", dt.Rows(0)("Adress").ToString())
ElseIf docRange.Text.Trim() = "Nm" Then
docRange.Text = docRange.Text.Replace("Nm", dt.Rows(0)("Name").ToString())
ElseIf docRange.Text.Trim() = "Bth" Then
docRange.Text = docRange.Text.Replace("Bth", dt.Rows(0)("Birthday").ToString())
End If
Next
doc.Save()
Catch ex As Exception
Response.Write(ex.Message)
Finally
doc.Close(missing, missing, missing)
DirectCast(word, Word._Application).Quit()
End Try
End If
End Sub
End Class
Screenshot