2
votes

I'm working in an application that interact with Word. We import documents in our database then allow the user to modify those documents using Microsoft Word via NetOffice.WordApi.

We are having some issues when the document is marked as read-only, everytime is opened, we get a 'Save As' word dialog that gives you the option of creating a temporal copy of the document to allow you to make changes in the document.

My question is as follow: How can remove the read-only word mark of the document and re-save the document without the mark? I can remove the option manually from word following the instructions in the link below, but I want to automatise that process by code.

The author would like you to open this as read-only

** PLEASE BEFORE ANSWER NOTICE THE FOLLOWING: It's not the read-only property in the file at windows level, it's not an issue with the windows permision attibute. The file attributes are NOT Read-only the property is inside the word document. So changing the windows file permision attributes by code doesn't work, actually the files are not read-only when you check the file properties in windows, it's a word attribute which means the author marked the document as read-only when he saved it and will stop you to modify the file using word (file can be changed using a different software, it just read-only for word). Please don't sent me links about how to change permissions in windows at that's not the case, check the link for more info. **

Many thanks for your time.

1
sounds like its a question of how you are saving in the NetOffice.WordApi?Daniel A. White
That doesn't really matter actually, the file is created in Word, then imported to our application by saving the content of the file in the db. The fix is meant to be implemented before you even actually open the file with NetOffice, at the moment you save the content of the file in the database. The idea would be that, after removing the mark, when you open the file with NetOffice you will not get the 'Save As' when you invoke Word for editing the file. The read-only option is assigned when you initially create and save the document using microsoft Word for the first time outside the app.Kymuweb

1 Answers

2
votes

The option you want to set is the Document.ReadOnlyRecommended property of the Word document.

When you open a Word document, you can actually set the ReadOnly property with the third argument in the Document.Open method. That argument won't, however, override the read-only recommended setting on a saved document. So if your document is saved with read-only recommended option, it will be opened as read-only when calling Document.Open.

So I think that you have two option:

Option 1

Set the Document.ReadOnlyRecommended to false before the Document is saved for the first time, similar to this

objDoc.ReadOnlyRecommended = false;


Option 2

If the document already is set to read-only recommended, you need to save the document as a new file with the Document.ReadOnlyRecommended property set to false using the Document.SaveAs2 method.

Your code might look like this:

        object missing = System.Reflection.Missing.Value;
        object readOnly = false;
        object fileName = @"C:\User\MyFile.docx";
        object newFileName = @"C:\User\MyNewFile.docx";

        var objApp = new Application();
        var objDoc = objApp.Documents.Open(ref fileName, ref missing, ref readOnly);

        if (objDoc.ReadOnlyRecommended)
        {
            objDoc.SaveAs2(ref newFileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref readOnly);
        }
        objDoc.Close();
        objApp.Quit();