|
Edit MS Word document from your application
I faced this problem when I was developing a small tool to write official Letters. My requirements were, letter template & content can be change any time and application will help to get only inputs like, sent to name, organization name, date etc. and after that when user press finish button, letter should be ready for print out along with predefine content & inputs.
To solve this I used MS VS 2005 with C#2.0.
Here are the steps I followed:
1.Add Microsoft.Office.Interop.Word reference to the project from COM Component. This will automatically add VBIDE, Microsoft.Office.Core along with Microsoft.Office.Interop.Word as references.
2.Now create a MS Word document with the fields <Name>, <Date>, <Subject> and letter body. Then place all 3 variables(I called these variable because these will be change automatically base on user input) where they belongs to.
3.Now design your form in VS IDE with require controls for above fields.
4.Now lets do some coding…..ya….. so here is my sample coding,
Header Files:
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Data.OleDb;
using System.Collections;
using Word = Microsoft.Office.Interop.Word; <- this is what I am talking about
Code behind the button:
private void btnlettergen_Click(object sender, EventArgs e)
{
// create offer letter
try
{
// Just to kill WINWORD.EXE if it is running
killprocess("winword");
// copy letter format to temp.doc
File.Copy("C:\\OfferLetter.doc", "c:\\temp.doc", true);
// create missing object
object missing = Missing.Value;
// create word application object
Word.Application wordApp = new Word.ApplicationClass();
// create word document object
Word.Document aDoc = null;
// create & define filename object with temp.doc
object filename = "c:\\temp.doc";
// if temp.doc available
if (File.Exists((string)filename))
{
object readOnly = false;
object isVisible = false;
// make visible word application
wordApp.Visible = false;
// open word document named temp.doc
aDoc = 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);
aDoc.Activate();
// Call FindAndReplace()function for each change
this.FindAndReplace(wordApp, "<Date>", dtpDate.Text);
this.FindAndReplace(wordApp, "<Name>", txName.Text.Trim());
this.FindAndReplace(wordApp, "<Subject>", txtSubject.Text.Trim());
// save temp.doc after modified
aDoc.Save();
}
else
MessageBox.Show("File does not exist.", "No File", MessageBoxButtons.OK, MessageBoxIcon.Information);
killprocess("winword");
}
catch (Exception)
{
MessageBox.Show("Error in process.", "Internal Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Create FindAndReplace():
private void FindAndReplace(Word.Application wordApp, object findText, object replaceText)
{
object matchCase = true;
object matchWholeWord = true;
object matchWildCards = false;
object matchSoundsLike = false;
object matchAllWordForms = false;
object forward = true;
object format = false;
object matchKashida = false;
object matchDiacritics = false;
object matchAlefHamza = false;
object matchControl = false;
object read_only = false;
object visible = true;
object replace = 2;
object wrap = 1;
wordApp.Selection.Find.Execute(ref findText, ref matchCase,
ref matchWholeWord, ref matchWildCards, ref matchSoundsLike,
ref matchAllWordForms, ref forward, ref wrap, ref format,
ref replaceText, ref replace, ref matchKashida, ref matchDiacritics,
ref matchAlefHamza, ref matchControl);
}
5.Now check out your temp.doc file at C:\, all varialble defiened under ‘<>’ are replaced with the input values. Hows that!
So, enjoy your coding….. happy coding!
|
|
|
|