在C#中编辑后无法打开文档文件 [英] Unable to open doc file after editing in C#

查看:146
本文介绍了在C#中编辑后无法打开文档文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了以下代码来编辑Word文档的书签.由于生成了文档,因此可以看到doc,但是如果我关闭该文档并再次尝试打开同一doc,则doc单词会出现错误,如下所示:

1.检查文件权限以获取文件/驱动器.
2.确保有足够的内存和磁盘空间.


私有void GenerateApplication()
{
对象readOnly = true;
对象修复=假;
对象bookMarkName ="applicantName";
对象bookMarkCompany ="firmName";
对象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;

字符串outputFile = @"C:\ Documents and Settings \ All Users \ Desktop \ ApplicationOf-" + txtApplicantName.Text +".docx";

bookMarkName = null;
bookMarkCompany = null;
bookMarkPost = null;

对象objSaveAsFile = outputFile;
对象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;
}

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;
}

推荐答案

自从我在C#中使用Word自动化以来已经有好几年了,但这是我想起的;看看是否相关:

当您将Text分配给现有BookMark的Range.Text属性时,这将有效地删除BookMark对象,因为BookMark本身包含在Range中.

然后,您要做的是先尝试检查书签是否确实存在,然后再尝试将其重置,然后测量"书签,根据您的测量结果创建一个新的范围,然后用新的范围重新创建它.文字值;此处显示了大约在2006年用于VSTO的此技术的示例:[
It has been years since I''ve worked with Word automation in C#, but this comes to mind; see if this is relevant:

When you assign Text to an existing BookMark''s Range.Text Property, that effectively removes the BookMark object, because the BookMark itself is included in the Range.

What you want to do, then, is first check if the BookMark actually exists before trying to reset it, and, then, "measure" the BookMark, create a new Range based on your measurements, then, re-create it with its new Text value; an example of this technique for VSTO circa 2006 is shown here: [^].


这篇关于在C#中编辑后无法打开文档文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆