Hi,
This is what i have done till now...
var myWord = new Microsoft.Office.Interop.Word.Application();
var myDoc = new Microsoft.Office.Interop.Word.Document();
object missing = System.Type.Missing;
try
{
object FileName = @"D:\Declaration.docx";
myDoc = myWord.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);
myDoc.Activate();
foreach (Microsoft.Office.Interop.Word.Range tempRange in myDoc.StoryRanges)
{
tempRange.Find.Text = "@DonorName";
tempRange.Find.Replacement.Text = "Sandeep D Shenoy";
object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
tempRange.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
tempRange.Find.Execute(ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref replaceAll,
ref missing, ref missing, ref missing, ref missing);
tempRange.Find.Text = "@Date";
tempRange.Find.Replacement.Text = "01-01-2013";
tempRange.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
tempRange.Find.Execute(ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref replaceAll,
ref missing, ref missing, ref missing, ref missing);
tempRange.Find.Text = "@Telephone";
tempRange.Find.Replacement.Text = "1234567890";
tempRange.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
tempRange.Find.Execute(ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref replaceAll,
ref missing, ref missing, ref missing, ref missing);
tempRange.Find.Text = "@Email";
tempRange.Find.Replacement.Text = "abd@gmail.com";
tempRange.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
tempRange.Find.Execute(ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref replaceAll,
ref missing, ref missing, ref missing, ref missing);
}
myDoc.Save();
myDoc.Close(ref missing, ref missing, ref missing);
myWord.Application.Quit(ref missing, ref missing, ref missing);
}
catch (Exception ee)
{
myDoc.Close(ref missing, ref missing, ref missing);
myWord.Application.Quit(ref missing, ref missing, ref missing);
}
I replaced 4 Texts from the document and replaced it with values, but this document has to be reused many times. So is there any way to bypass that ?
|