我已经编写了以下代码来编辑 Word 文档的书签。生成文档时,文档是可见的,但是如果我关闭相同的文档并尝试再次打开相同的文档,则 Word 文档会给出以下错误:
1.检查文件/驱动器的文件权限。2.确保有足够的内存和磁盘空间。
private void GenerateApplication()
{
object readOnly = true;
object repair = false;
object bookMarkName = "applicantName";
object bookMarkCompany = "firmName";
object bookMarkPost = "postName";
fielName = @"C:\Documents and Settings\AllUsers\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;
}