“You are not allowed to edit this selection becaus

2019-04-06 04:13发布

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.

8条回答
Fickle 薄情
2楼-- · 2019-04-06 04:43

For me, the issue was similar to Tim Dols answer, but I needed to unlock the content of the content control., which is the LockContents property: mycontentcontrol.LockContents = False

For @CrazyIvan1974, the problem with that solution is that Add creates a new document. if you point at an existing document when using Add, it doesn't load the document, it creates a new document using the original as a template. That can disconnect templates and add-ins, really messing you up if you save over the original

查看更多
手持菜刀,她持情操
3楼-- · 2019-04-06 04:45

When you open a document, specify that it should not be opened as read-only

object readOnly = false; 
doc = word.Documents.Open(ref path, ref miss, ref readOnly, ...);
查看更多
男人必须洒脱
4楼-- · 2019-04-06 04:47

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.

查看更多
一夜七次
5楼-- · 2019-04-06 04:53

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.

查看更多
虎瘦雄心在
6楼-- · 2019-04-06 04:56

This has been happening to me for the past two days (while creating a dotm template) and what fixed it for me was to create a new normal.dotx! Don't know if that will work for others or not, but it did for me!

查看更多
Deceive 欺骗
7楼-- · 2019-04-06 05:01

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.

查看更多
登录 后发表回答