Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"You are not allowed to edit this selection because it is protected." but only since Office 2013?

We've had these few lines of code running happily in our applications for several years (and in several versions of Office, 2003, 2007, 2010 etc). Purpose is to perform a kind of a mail merge in a Word document, substituting the field placeholders with names, addresses etc from a database:

    Dim w As Word.Application
    Dim d As Microsoft.Office.Interop.Word.Document = Nothing

...

    Dim f As Microsoft.Office.Interop.Word.Field
    For Each f In d.Fields
        f.Select()
        If fieldName = w.Selection.Text Then
            f.Result.Text = value
        End If
    Next

However a user running Office 2013 reports this error on the line f.Result.Text = value:

System.Runtime.InteropServices.COMException (0x800A17EC): You are not allowed to edit this selection because it is protected.

So, this is only happening when the user is running Office 2013 and there's very little online help for this error.

No part of the document is protected, and the user can edit the document directly in Word without any problem.

like image 299
hawbsl Avatar asked Jul 11 '13 13:07

hawbsl


People also ask

How do I change when a selection is locked in Word?

This modification is not allowed because the selection is locked. To return to the task pane and find a place where you have permission to edit, do the following: On the Review tab, in the Protect group, click Protect Document, and then click Restrict Formatting and Editing.

Why Cannot I edit or type on word it states selection is locked?

Solution: In the Review ribbon tab, click Restrict Editing (under Protect) and see if protection has been applied. If it has, then unprotect it.


5 Answers

We had some C# automation that worked fine with Word 2007/2010, but stopped with Word 2013 with the same "You are not allowed ..." warning.

Following steps on this site solved the issue.

Basically there are two settings to check:

  • File – Options – General. Uncheck “Open E-Mail attachments and other uneditable files in reading view”
  • File – Options – Trust Center – Trust Center Settings. Select Protected View, then clear all the checkboxes.
like image 165
mp31415 Avatar answered Oct 02 '22 08:10

mp31415


Tried most of the suggestions above but I found this fixed the problem. We were opening the doc as a template in read-only with a password. So couldn't use 'Add'

Documents.Open(strTemplateDoc, ReadOnly:=True, PasswordDocument:=strDocPassword, Visible:=False)

Setting the View.Type to wdNormalView stopped the error "You are not allowed to edit this selection because it is protected"

wdDocPage.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdNormalView

Thanks to all the others for their suggestions - they helped a lot.

like image 27
pjlusenet Avatar answered Oct 02 '22 08:10

pjlusenet


In desperation, trawling for answers even in blog posts and discussions far removed from this particular error it seems there's been a change in Office 2013 to the default treatment of the ReadingLayout.

Introducing the line w.ActiveWindow.View.ReadingLayout = False seems to have solved our problem.

like image 25
hawbsl Avatar answered Oct 02 '22 09:10

hawbsl


You don't specify how the document is opened, but a problem I had was resolved by following the answer accepted on this question.

Switching from WordApplication.Documents.Open() to WordApplication.Documents.Add() resolved the issue for my application.

like image 20
CrazyIvan1974 Avatar answered Oct 02 '22 08:10

CrazyIvan1974


In my case, this error was caused by the presence of content controls with .LockContentControl == true.

To work around this issue, I built an IEnumerable<ContentControl> of the content controls with this property set to true, and set .LockContentControl = false. Now I can .InsertColumnsRight() without a problem. Then I restore the .LockContentControl = true for all content controls in my collection.

like image 44
Tim Dol Avatar answered Oct 02 '22 09:10

Tim Dol