
Introduction
Introduction
Quite simple way of creating the MS Word document through C# language.
Background
Minimum understanding of C# language buzz
Using the code
Create a new project, for simplicity, create a Windows application, go ahead and right click on References in the Solution Explorer, and select Add Reference� When the Add Reference window comes up, select the COM tab. This will list all Component names which are available on your machine. Since we are going to use MS Excel, you will scroll down until you find: Microsoft Excel 11.0 Object Library. You can even do the same thing PIAs.
I have created a very simple wrapper class for Word object model . The code will be given below. This project also has the error provider functionality to make sure that user will enter values for all the fields.
Create button click event. Here we checking whether all fields are filled and then call the CreateFile()
function in Word wrapper class
private void btnCreate_Click(object sender, System.EventArgs e)
{
WordAuto word = new WordAuto();
ArrayList array = new ArrayList();
bool filled = true;
if ( ( txtName.Text).Trim().Length.Equals(0) )
{
errorProvider.SetError(txtName,"cannot be empty" );
filled = false;
}
if( ( txtAge.Text ).Trim().Length.Equals(0) )
{
errorProvider.SetError( txtAge,"cannot be empty" );
filled = false;
}
if( ( txtDesignation.Text ).Trim().Length.Equals(0) )
{
errorProvider.SetError( txtDesignation,"cannot be empty" );
filled = false;
}
if( ( txtPlace.Text ).Trim().Length.Equals(0) )
{
errorProvider.SetError( txtPlace,"cannot be empty" );
filled = false;
}
if( ( txtEmail.Text ).Trim().Length.Equals(0) )
{
errorProvider.SetError( txtEmail,"cannot be empty" );
filled = false;
}
if( ( txtCompany.Text ).Trim().Length.Equals(0) )
{
errorProvider.SetError( txtCompany,"cannot be empty" );
filled = false;
}
if ( filled == true )
{
array.Insert(0,txtName.Text);
array.Insert(1,txtAge.Text);
array.Insert(2,txtDesignation.Text);
array.Insert(3,txtCompany.Text);
array.Insert(4,txtPlace.Text);
array.Insert(5,txtEmail.Text);
word.CreateFile(array);
}
}
Doc File creation Code
public void CreateFile(ArrayList array)
{
object missing = System.Reflection.Missing.Value;
object fileName = "normal.dot";
object newTemplate = false;
object docType = 0;
object isVisible = true;
wordApp = new Word.ApplicationClass();
wordDoc = wordApp.Documents.Add(ref fileName, ref newTemplate, ref docType, ref isVisible);
wordApp.Visible = true;
wordDoc.Activate();
wordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
wordApp.Selection.Font.Bold = (int)Word.WdConstants.wdToggle;
wordApp.Selection.TypeText("Name : "+ array[0].ToString());
wordApp.Selection.TypeParagraph();
wordApp.Selection.TypeText("Age : "+ array[1].ToString());
wordApp.Selection.TypeParagraph();
wordApp.Selection.TypeText("Designation : " + array[2].ToString());
wordApp.Selection.TypeParagraph();
wordApp.Selection.TypeText("Company : " + array[3].ToString());
wordApp.Selection.TypeParagraph();
wordApp.Selection.TypeText("Place : " + array[4].ToString());
wordApp.Selection.TypeParagraph();
wordApp.Selection.TypeText("Email : "+array[5].ToString());
wordApp.Selection.TypeParagraph();
}
Conclusion
The interop assemblies provide a lot of options for working with Word data, both reading and writing.I hope this article gives you a head start in controlling word from .NET and C#. Some experiment with the object model will help you to get that hands on........
History
First Draft 29 December 2006