private void CreateWordDocument(object fileName, object saveAs)
{
//Set Missing Value parameter - used to represent a missing value when
//calling methods through interop
object missing = System.Reflection.Missing.Value;
Word.Application wordApp = new Word.ApplicationClass();
Word.Document aDoc = null;
//check to see that file exists
if (File.Exists((string)fileName))
{
DateTime today = DateTime.Now;
object readOnly = false;
object isVisible = false;
//Set Word to be not visible
wordApp.Visible = false;
//Open the word document
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);
//Activate the document
aDoc.Activate();
//Example of writing to the start of a document
aDoc.Content.InsertBefore("This is at the beginning /r/n/r/n");
//Example of writing to the end of a document
aDoc.Content.InsertAfter("/r/n/r/nThis is at the end");
}
else
{
MessageBox.Show("File does not exits.");
return;
}
aDoc.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);
aDoc.Close(ref missing, ref missing, ref missing);
MessageBox.Show("File created.");
}