none
Enable editing of rich text box in locked portion of document RRS feed

  • Question

  • I have a Word add-in that is getting a rich text box inserted into a bookmark from the server along with a table that serves at a title bar above it. This part of the document they are inserted to is locked, but I would like to be able to change font size/ style in the rich text box only, not the table, so I am trying to programmatically insert section breaks to mixed success. The area unlocks but apparently you can only insert a section break above tables and not below, so the user is able to edit the table/ title bar. The rich text box does not have a range property like the table.

    Is there a way to keep the rich text box locked but still be able to change font/size? Is there an alternative "hack" to what I have mentioned I am doing above?

    Thank you!

    Friday, October 23, 2015 9:01 PM

Answers

  • What I ended up doing is looping through the text in each of the paragraphs in the bookmark and if the text there was "Click here to enter text.\r", the default rich text content control text, I inserted a paragraph before it. Then, I looped through the paragraphs again (there is an additional one now), found the text "Click here to enter text.\r", and inserted a  section break in that paragraphs range.

        public void InsertBreakIntoBookmark(Bookmark bookmark)
            { 
                UnlockDocument();

                int i = bookmark.Range.Paragraphs.Count;

                Table tbl2 = GetTableByBookmarkName(bookmark, 2);
                
                object Type = Microsoft.Office.Interop.Word.WdBreakType.wdSectionBreakContinuous;
                Range r1;
                for (int j = 1; j <= bookmark.Range.Paragraphs.Count; j++)
                {
                    if (bookmark.Range.Paragraphs[j].Range.Text == "Click here to enter text.\r")
                    {
                        Range r = bookmark.Range.Paragraphs[j].Range;
                        r.InsertParagraphBefore();
                        break;

                    }
                }

                for (int j = 1; j <= bookmark.Range.Paragraphs.Count; j++ )
                {
                    if (bookmark.Range.Paragraphs[j].Range.Text == "Click here to enter text.\r")
                    {
                        r1 = bookmark.Range.Paragraphs[j].Range;
                        r1.InsertBreak(ref Type);
                        
                        //LockRichTextBoxByBookmarkName(bookmark);
                        break;
                    }
                }
                Range insertRange2 = tbl2.Range;
                insertRange2.InsertBreak(ref Type);
                LockDocument();
            }

    • Marked as answer by Luke415 Tuesday, October 27, 2015 8:25 PM
    Tuesday, October 27, 2015 8:25 PM

All replies

  • Hi Luke

    Your problem description is missing some important information:

    1. What is this "rich text box", more exactly? Word doesn't have anything called "rich text box" so it's important you explain to us exactly what you mean

    2. Which version of Word?

    3. Please be more specific about "locked". I assume this is some kind of document protection, but what kind?


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Sunday, October 25, 2015 5:13 PM
    Moderator
  • Hi Cindy, 

      Thank you for the reply and I apologize for not being clear.

    1) It is the "Rich Text Content Control" found in the Developer tab in Word.

    2) I am using Word 2010.

    3) I am locking the document using:

    _document.Protect(WdProtectionType.wdAllowOnlyFormFields, ref _noReset, ref _password, ref _useIrm, ref _enforceStyleLock);

    Please let me know if I can provide more information.

    Thank you!


    • Edited by Luke415 Monday, October 26, 2015 12:45 PM
    Monday, October 26, 2015 12:44 PM
  • Hi Luke

    Thank you for the additional information. You'll notice, if you try this in Word as a user, that it's not possible to use formatting in a Rich Text Content Control in a document locked as a form.

    Is locking as a form necessary? Or could you use the "ReadOnly" type of protection that will allow formatting?

    Why are you using content controls at all if you already have bookmarks?

    A RT CC does have a Range and you can move before/after to insert section breaks. Here's some sample VBA code to demonstrate. The "before" part is the tricky one, as it depends on whether the control as at the beginning of a paragraph (first character) or not. You need only -1 if it's not, otherwise -2 (which will put it at the end of the preceding paragraph). In these positions it's possible to insert section breaks.

        Dim cc As word.ContentControl
        Dim rngCC As word.Range, rngBeforeCC As word.Range, rngAfterCC As word.Range
        
        Set cc = ActiveDocument.Contentcontrols(1)
        Set rngCC = cc.Range
        Set rngBeforeCC = rngCC.Duplicate
        Set rngAfterCC = rngCC.Duplicate
        
        rngBeforeCC.Collapse wdCollapseStart
        rngBeforeCC.MoveEnd wdCharacter, -2
        rngAfterCC.Collapse wdCollapseEnd
        rngAfterCC.MoveStart wdCharacter, 1
        rngAfterCC.Select
    

    To get a Range after a table, something like this:

       Word.Range rngTbl = theTable.Range;
       object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
       rngTbl.Collapse(ref oCollapseEnd);  //after the table

    Note that this will be the paragraph mark following the table and this paragraph mark is required and directly linked to the table. It holds some of the table formatting/structuring/layout information.


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Monday, October 26, 2015 3:24 PM
    Moderator
  • Hi Cindy,

    This is my C# code based on your answer:

        Range insertRange = rtcc.Range;
                Range insertRangeBefore = insertRange.Duplicate;
                Range insertRangeAfter = insertRange.Duplicate;
                insertRangeBefore.Collapse(Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseStart);
                insertRangeBefore.MoveEnd(Microsoft.Office.Interop.Word.WdUnits.wdCharacter,-2);
                insertRangeAfter.Collapse(Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd);
                insertRangeAfter.MoveStart(Microsoft.Office.Interop.Word.WdUnits.wdCharacter, 1);
                insertRangeAfter.Select();

                insertRangeBefore.InsertParagraphBefore();

                object Type = Microsoft.Office.Interop.Word.WdBreakType.wdSectionBreakContinuous;
                insertRangeAfter.InsertBreak(ref Type);
                insertRangeBefore.InsertBreak(ref Type);

                LockDocument();

    However, I do not get the section breaks inserted. Any thoughts?
    Monday, October 26, 2015 7:45 PM
  • What I ended up doing is looping through the text in each of the paragraphs in the bookmark and if the text there was "Click here to enter text.\r", the default rich text content control text, I inserted a paragraph before it. Then, I looped through the paragraphs again (there is an additional one now), found the text "Click here to enter text.\r", and inserted a  section break in that paragraphs range.

        public void InsertBreakIntoBookmark(Bookmark bookmark)
            { 
                UnlockDocument();

                int i = bookmark.Range.Paragraphs.Count;

                Table tbl2 = GetTableByBookmarkName(bookmark, 2);
                
                object Type = Microsoft.Office.Interop.Word.WdBreakType.wdSectionBreakContinuous;
                Range r1;
                for (int j = 1; j <= bookmark.Range.Paragraphs.Count; j++)
                {
                    if (bookmark.Range.Paragraphs[j].Range.Text == "Click here to enter text.\r")
                    {
                        Range r = bookmark.Range.Paragraphs[j].Range;
                        r.InsertParagraphBefore();
                        break;

                    }
                }

                for (int j = 1; j <= bookmark.Range.Paragraphs.Count; j++ )
                {
                    if (bookmark.Range.Paragraphs[j].Range.Text == "Click here to enter text.\r")
                    {
                        r1 = bookmark.Range.Paragraphs[j].Range;
                        r1.InsertBreak(ref Type);
                        
                        //LockRichTextBoxByBookmarkName(bookmark);
                        break;
                    }
                }
                Range insertRange2 = tbl2.Range;
                insertRange2.InsertBreak(ref Type);
                LockDocument();
            }

    • Marked as answer by Luke415 Tuesday, October 27, 2015 8:25 PM
    Tuesday, October 27, 2015 8:25 PM