Hi PRA,
Refer the below sample.
C#
using System;
using System.Data;
using Word = Microsoft.Office.Interop.Word;
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.SaveAs(@"C:\Users\Test\Desktop\" + dt.Rows[0]["Name"].ToString() + ".doc", missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
}
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
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.SaveAs("C:\Users\Test\Desktop\" & dt.Rows(0)("Name").ToString(), missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing)
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
It will keep original file safe without writting any thing to it and Saves the document with a new name or format in the provided path.