3
Answers

Unable to open doc file after editing in C#

Ameya Joshi

Ameya Joshi

10y
951
1
I have written following Code to edit bookmarks of Word document., as document is generated doc is visible but if i close the same and tried to open the same doc again the word doc gives error as :

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;
        }
Answers (3)