简体   繁体   English

如何使用c#删除word文档的readonly属性?

[英]How can I remove the readonly attribute of a word document using c#?

I'm using office 2013, and I used the code below to open a word document: 我正在使用office 2013,我使用下面的代码打开word文档:

object fileName = FD.FileName;
object readOnly = false;
object isVisible = true;
WordApp.Visible = true;
aDoc = WordApp.Documents.Open(ref fileName, ref missing,
ref readOnly, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref isVisible, ref missing, ref missing,
ref missing, ref missing);
aDoc.Activate();

How can I enable editing for some read only word files opened in my c# application? 如何为在c#应用程序中打开的一些只读单词文件启用编辑?

Actually it's nothing to do with Office-interop, ReadOnly is a file attribute of that file. 实际上它与Office-interop无关, ReadOnly是该文件的文件属性 You could remove this by setting its FileAttribute to FileAttributes.Normal before you open the file. 您可以通过它设置删除此FileAttributeFileAttributes.Normal您打开文件之前。

You can try the code below: 您可以尝试以下代码:

string fileName = FD.FileName;
File.SetAttributes(fileName, FileAttributes.Normal);

aDoc = WordApp.Documents.Open(fileName, Visible: isVisible);
aDoc.Activate();

Remember, if you want to set it back to ReadOnly after you close the file, add the line below after you call aDoc.Close() : 请记住,如果要在关闭文件后将其设置回ReadOnly ,请在调用aDoc.Close()后添加以下行:

File.SetAttributes(fileName, FileAttributes.ReadOnly);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM