Amit Shrivastava

Amit Shrivastava

  • NA
  • 293
  • 210.4k

How to create a word documet using c#

Feb 11 2015 4:58 AM
i have write code but parameter count is null please check it
 
 
Object oMissing = System.Reflection.Missing.Value;
//Object oTemplatePath = "D:\\Projects\\AccidentClaimSystem\\Template\\MyTemplate.dotx";
//Object oTemplatePath = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\Template\\MyTemplate.dotx";
Object oTemplatePath;
if (txtFileName.Text != "" && txtFileName.Text != string.Empty)
{
oTemplatePath = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\Template\\" + txtFileName.Text;
}
else
{
return;
}
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
Document wordDoc = new Document();

wordDoc = wordApp.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);
//try
//{
//System.Data.DataTable dt = objClientDAL.FormofAuthority();
if (txtName.Text != string.Empty && txtName.Text != "")
{
//objClientBAL.ClaimId = 2;
//objClientBAL.SearchIn = "L001";
//objClientBAL.ClaimId = Convert.ToInt64(txtName.Text);
//objClientBAL.SearchIn = txtFileName.Text;
//System.Data.DataSet ds = objClientDAL.FormofAuthority(objClientBAL);
//if (ds != null && ds.Tables.Count > 0)
//{
foreach (Field myMergeField in wordDoc.Fields)
{
Range rngFieldCode = myMergeField.Code;
String fieldText = rngFieldCode.Text;
// ONLY GETTING THE MAILMERGE FIELDS
if (fieldText.StartsWith(" MERGEFIELD"))
{
// THE TEXT COMES IN THE FORMAT OF
// MERGEFIELD MyFieldName \\* MERGEFORMAT
// THIS HAS TO BE EDITED TO GET ONLY THE FIELDNAME "MyFieldName"
Int32 endMerge = fieldText.IndexOf("\\");
Int32 fieldNameLength = fieldText.Length - endMerge;
String fieldName = fieldText;//.Substring(11, endMerge - 11);
//String fieldName = fieldText.Replace("MERGEFIELD","").Replace("\\* MERGEFORMAT","").Replace('"',"");
// GIVES THE FIELDNAMES AS THE USER HAD ENTERED IN .dot FILE
fieldName = fieldName.Trim();
// **** FIELD REPLACEMENT IMPLEMENTATION GOES HERE ****//

// THE PROGRAMMER CAN HAVE HIS OWN IMPLEMENTATIONS HERE

if (fieldName.Contains("Client_ClaimentNo"))
{
myMergeField.Select();

//if (ds.Tables[0].Rows[0]["Client_ClaimentNo"].ToString() != "")
//{
wordApp.Selection.TypeText("5");
//}
//else
//{
// wordApp.Selection.TypeText(" ");
//}
}

}
}
string FileName = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "/Template/" + txtFileName.Text + "." + "docx";

wordDoc.SaveAs(FileName);
wordApp.Documents.Open(FileName);

//}

Answers (2)