简体   繁体   中英

Word Interop Error - "This method or property is not available because the current selection is in a limited editing zone"

I have an application manages some documents using MS Word Interop, and works perfectly on one computer, but fails on another.

Here's what the code does, and what happens:

Microsoft.Office.Interop.Word.Document templateDoc = winWord.Documents.Open(ref 
          templateFileName, ReadOnly: true, Visible: true);

templateDoc.Activate();
winWord.ActiveDocument.Content.NoProofing = 1;

templateDoc.Words.Last.InsertBreak(Microsoft.Office.
      Interop.Word.WdBreakType.wdPageBreak);

When I get to the last instruction - the insert page break - I get an error on one of the machines, that states: "This method or property is not available because the current selection is in a limited editing zone."

On the other machine the insert is fine. I am using the same template file in both cases.

Since this varies between the 2 computers I also checked to see which version of MS Word is installed. The machine that does not get the error is running Word version 15726.20262. The machine that gets the error is running Word version 15928.20198.

*Both machines were set up using the same MS365 subscription, and both machines state that no further updates are available for the installed Word.

I also checked the versions of the Microsoft.Office.Interop.Word nuget package. Both machines are using 15.0.4797.1004 - the latest stable version.

Can this be fixed?

As noted in my answer to user09938 above, the answer to this came from the question they asked.
It appears to me that in the latest Word kernel, Microsoft has changed the effect of the ReadOnly property. In the past I was able to open a file as ReadOnly with Word Interop, edit it and then save it to a new file name. I would not have been able to save it to the original name - because of the ReadOnly state. Now I can't edit it once I open it as ReadOnly. My solution was to copy my original prototype (called template in my code above, but actual just a.docx prototype) and open it with Word Interop with ReadOnly: false. The rest of my editing remains the same as before. When I am done with all the editing I need to delete the copy - so that I don't run into a conflict when I next run the application.

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