Requirement:To change some worde of a word document template and save it at my document of the user.
By this application you can change the word application by selecting the gridview and the document template from the dropdownlist.
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Office;
using Microsoft.Office.Interop.Word;
using System.IO;
using System.Data;
using System.Data.SqlClient;
namespace ProjectForReplaceWord
{
public partial class _Default : System.Web.UI.Page
{
Microsoft.Office.Interop.
Microsoft.Office.Interop.
string Vendor = string.Empty;
string TenderNo = string.Empty;
string TenderDatedOn = string.Empty;
//int ddlindex = string.Empty;
string VendorAddress = string.Empty;
string ProductType = string.Empty;
string DueOn = string.Empty;
string ValidFrom = string.Empty;
string ValidUpTo = string.Empty;
string SenderName = string.Empty;
string Designation = string.Empty;
string ToAddress = string.Empty;
string Subject = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string connectionString = "Data Source=ServerName;Initial Catalog=DatabaseName;
SqlConnection cnn = new SqlConnection(
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM [DataBaseName].[dbo].[
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
ddlTemplate.Items.Insert(0, "-Select-");
ddlTemplate.Items.Add("
ddlTemplate.Items.Add("
ddlTemplate.Items.Add("
ddlTemplate.DataBind();
}
}
protected void GridView1_
{
GridViewRow row = GridView1.SelectedRow;
int index = row.RowIndex;
Vendor = row.Cells[1].Text;
TenderNo = row.Cells[2].Text;//Label4.
TenderDatedOn = row.Cells[3].Text;
int ddlindex=ddlTemplate.
VendorAddress = row.Cells[4].Text;
ProductType = row.Cells[5].Text;
DueOn = row.Cells[6].Text;
ValidFrom = row.Cells[7].Text;
ValidUpTo = row.Cells[8].Text;
SenderName = row.Cells[9].Text;
Designation = row.Cells[10].Text;
ToAddress = row.Cells[11].Text;
Subject = row.Cells[12].Text;
string TemplateDoc = ddlTemplate.SelectedItem.
string NewDoc = TemplateDoc+ "_"+Vendor +Designation+ ".docx";
CreateWordDocument(Server.
}
protected void FindAndReplace(Microsoft.
{
object matchCase = true;
object matchwholeWords = true;
object matchWildCards = false;
object matchSoundlike = false;
object nmatchAllWordForms = false;
object forward = true;
object format = false;
object matchKashida = false;
object matchDiacritics = false;
object matchAlefHamza = false;
object matchControl = false;
object readOnly = false;
object visible = true;
object replace = 2;
object wrap = 1;
wordApplication.Selection.
}
private void CreateWordDocument(object filename, object SaveAs)
{
object missing = System.Reflection.Missing.
if (File.Exists((string)filename)
{
DateTime today = DateTime.Now;
object readOnly = false;
object isVisible = false;
wordApp.Visible = false;
wordDoc = wordApp.Documents.Open(ref filename, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
wordDoc.Activate();
this.FindAndReplace(wordApp, "<Subject>", Subject);
this.FindAndReplace(wordApp, "<tender no>", TenderNo);
this.FindAndReplace(wordApp, "<tender Date>", TenderDatedOn);
this.FindAndReplace(wordApp, "<due date>", DueOn);
this.FindAndReplace(wordApp, "<Vendor>", Vendor);
this.FindAndReplace(wordApp, "<Start Date> ", ValidFrom);
this.FindAndReplace(wordApp, "<End Date>", ValidUpTo);
this.FindAndReplace(wordApp, "<Distributor Address>", VendorAddress);
this.FindAndReplace(wordApp, "<Product Type> ",ProductType);
this.FindAndReplace(wordApp, "<Sender Name>", SenderName);
this.FindAndReplace(wordApp, "<Designation>", Designation);
this.FindAndReplace(wordApp, "<XYZ Private Limited>", Vendor);
wordDoc.SaveAs(ref SaveAs, 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);
//see MyDocument of the User.
//wordDoc.Close(ref missing, ref missing, ref missing);
// wordDoc.Save();
}
else
{
TextTemplate.Text = "File is not exist";//label1.text="File not exist";
}
First document say :AuthorisationLetter.docx
Sub : <Subject>.
Ref : Tender No. <tender no>Dated<tender Date>Due on<due date>
.
Dear Sir
1. We ABC Pvt. Ltd, hereby confirm that M/s. <Vendor>, a company having its principal place of business at Distributor Address is an authorized independent distributor of our company, in which capacity it may bid, negotiate and conclude the contract with you for ProductType traded by us.
2. No company or firm or individual, other than M/s. <Vendor>, are entitled to bid, negotiate and conclude the contract in regard to this business in such a capacity for the specific Tender.
3. This distribution arrangement is valid from <Start Date>to<End Date>.
Thanking you,
Yours faithfully
<SenderName>
<Designation>
ABC Private Limited
Here, the words to be changed is in< >.
Note:I got the code from one url at internet and as per that I wrote this new approach.
Thanks & Regards
Pradeep
Bangalore
No comments:
Post a Comment