1.Check file permissions for file/ drive.
2.make sure there is sufficient memory and disk space.
private void GenerateApplication()
{
object readOnly = true;
object repair = false;
object bookMarkName = "applicantName";
object bookMarkCompany = "firmName";
object bookMarkPost = "postName";
fielName = @"C:\Documents and Settings\All Users\Desktop\OperateWordFromC#.dotx";
objDoc = wordApp.Documents.Open(ref fielName, ref missingValuesValues, ref readOnly, ref missingValuesValues, ref missingValuesValues,
ref missingValuesValues, ref missingValuesValues, ref missingValuesValues, ref missingValuesValues,
ref missingValuesValues, ref missingValuesValues, ref missingValuesValues, ref repair, ref missingValuesValues, ref missingValuesValues, ref missingValuesValues);
objDoc.Activate();
objDoc.Bookmarks.get_Item(ref bookMarkName).Range.Text = txtApplicantName.Text;
objDoc.Bookmarks.get_Item(ref bookMarkCompany).Range.Text = txtAppliedCompany.Text;
objDoc.Bookmarks.get_Item(ref bookMarkPost).Range.Text = txtAppliedPost.Text;
string outputFile = @"C:\Documents and Settings\All Users\Desktop\ApplicationOf-" + txtApplicantName.Text + ".docx";
bookMarkName = null;
bookMarkCompany = null;
bookMarkPost = null;
Object objSaveAsFile = outputFile;
Object objFileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;
wordApp.ActiveDocument.SaveAs(ref objSaveAsFile, ref repair, ref missingValuesValues, ref missingValuesValues, ref missingValuesValues,
ref missingValuesValues, ref missingValuesValues, ref missingValuesValues, ref missingValuesValues, ref missingValuesValues,
ref missingValuesValues, ref missingValuesValues, ref missingValuesValues, ref missingValuesValues, ref missingValuesValues, ref missingValuesValues);
wordApp.Visible = true;
}
Glen LewisPosted Jun 26, 2014, 5:37 AM
You need to use Add instead of Open, try the following:
// Remove this ...
var objDoc = wordApp.Documents.Open(ref fielName, ref missingValuesValues, ref readOnly, ref missingValuesValues, ref missingValuesValues,
ref missingValuesValues, ref missingValuesValues, ref missingValuesValues, ref missingValuesValues,
ref missingValuesValues, ref missingValuesValues, ref missingValuesValues, ref repair, ref missingValuesValues, ref missingValuesValues, ref missingValuesValues);
// Add this ...
var objDoc = wordApp.Documents.Add(Template: ref fielName);
With this you can just specify the output file name with ".docx" extension and it will work:
wordApp.ActiveDocument.SaveAs(FileName: ref objSaveAsFile);
Also if interested another way that you can try is with this C# component for word files processing, it has a nice API for processing DOCX files in C# apps, for example try the following:
private static void GenerateApplication()
{
object bookMarkName = "applicantName";
object bookMarkCompany = "firmName";
object bookMarkPost = "postName";
string fielName = @"C:\Documents and Settings\All Users\Desktop\OperateWordFromC#.dotx";
var doc = DocumentModel.Load(fielName);
doc.Bookmarks["applicantName"].GetContent(false).LoadText(txtApplicantName.Text);
doc.Bookmarks["firmName"].GetContent(false).LoadText(txtAppliedCompany.Text);
doc.Bookmarks["postName"].GetContent(false).LoadText(txtAppliedPost.Text);
string outputFile = @"C:\Documents and Settings\All Users\Desktop\ApplicationOf-" + txtApplicantName.Text + ".docx";
doc.Save(outputFile);
System.Diagnostics.Process.Start(outputFile);
}
Ameya JoshiPosted Sep 30, 2013, 7:31 AM
Posted Sep 30, 2013, 1:43 AM