简体   繁体   中英

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:

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?

Actually it's nothing to do with Office-interop, ReadOnly is a file attribute of that file. You could remove this by setting its FileAttribute to FileAttributes.Normal before you open the file.

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() :

File.SetAttributes(fileName, FileAttributes.ReadOnly);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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