Set Section Object Border return an Error: This method or property is not available because the current selection is locked for format changes. RRS feed

  • Question

  • Hi, i'm tryng to set a section border of a section from another. 

    This is my code: 

    protected void ImpostaBorder(Document dc) { int intsection = 1; foreach (Microsoft.Office.Interop.Word.Section sec in dc.Sections) { if (intsection <= this.Control.Range.Sections.Count) { try{ this.setBorder(this.Control.Range.Sections[intsection], sec); } }




    private void setBorder(Microsoft.Office.Interop.Word.Section secDest, Microsoft.Office.Interop.Word.Section sec)
     Borders borders = sec.Borders;
                Borders secBorders = secDest.Borders;
                Border borderL = borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderLeft];
                Border dstBorderL = secBorders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderLeft];
                borderL.Color = dstBorderL.Color;
     	    borderL.ColorIndex = dstBorderL.ColorIndex;
                borderL.LineWidth = dstBorderL.LineWidth;
                borderL.LineStyle = dstBorderL.LineStyle;

    But when i've tried to set the color property or another one it returned this exception:

    Error: This method or property is not available because the current selection is locked for format changes. 

    What is the problem? how i can fix it? 

    • Edited by M4TT3 Monday, June 30, 2014 12:09 PM
    Monday, June 30, 2014 10:48 AM

All replies

  • Hi Matt

    1. Since your code is out-of-context we don't have enough information to follow what objects are being used:

        this.setBorder(this.Control.Range.Sections[intsection], sec);

    What's "this", more specifically: this.Control?

    2. Has Format Restriction or some other kind of document protection been activated in the document? The error message would seem to indicate so. If that is the case, you must first remove the protection before you can make the change.

    Cindy Meister, VSTO/Word MVP, my blog

    Monday, June 30, 2014 3:04 PM
  • Hi Cindy

    "this" is a content control

    I've no restriction on my document, indeed i can do the same operation without problems on orientation and page width. 

     this.Control.Range.Sections[intsection].PageSetup.Orientation = sec.PageSetup.Orientation;
     this.Control.Range.Sections[intsection].PageSetup.PageHeight = sec.PageSetup.PageHeight;
     this.Control.Range.Sections[intsection].PageSetup.PageWidth = sec.PageSetup.PageWidth;
    The problems was started with borders, i can "Get" value, but no "set". 

    i've noticed that on the object "Borders" i've the property "Enable" set to "-1". 

    • Edited by M4TT3 Monday, June 30, 2014 4:11 PM
    Monday, June 30, 2014 3:22 PM
  • Hi Matt

    I still don't understand where "this" is deriving from. And as far as I know, a Microsoft.Office.Interop.Word Document object has no Control property. It's not possible to provide optimal assistance if we can't be sure what we're dealing with...

    If format protection is activated that won't prevent you from changing the page orientation or page width. But it will prevent you from changing formatting such as font, borders, alignment...

    Check what the Document.EnforceStyle property returns. If it's True (you might get -1 using C#) then Format restriction IS activated.

    Cindy Meister, VSTO/Word MVP, my blog

    Monday, June 30, 2014 3:33 PM
  • hi Cindy,


    So "this" is a word "content control" and dc.EnforceStyle returns False. 

    Monday, June 30, 2014 3:42 PM
  • Hi Matt

    Since what you're working with is "wrapped" and we can't see what these things actually are, nor how they're derived, it's close to impossible for us to trouble-shoot.

    At this point, what I'd try is, as an end-user in the Word application, to open a document in the state it is in when your code hits this spot. Then try to apply the border formatting manually. Perhaps Word's UI will give you a more informative message, if it feels this can't be done. Or, if you can do it, then you know your code is putting the document in this state.

    Also, since the error message mentions "the current selection" make sure you know WHERE the selection is and that you have the same one before you try applying border formatting to the section. No where in the code you show us is any indication about WHAT the current selection is.

    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, July 1, 2014 3:37 PM
  • Hi Cindy,

    Word allow me to modify border manually, there is a way to unlock the selection programmatically? 

    How i can check if i'm on the right selection

    i've done this before set the border, but doesn't change anything.


    another things that i've noticed, is that the borders's control property enable is set to 0 and i can't change it for the same reason "..selection locked for format change".

    Wednesday, July 2, 2014 7:16 AM