none
Empty line at the bottom of the RichTextContentControl whenever I insert a table

    Question

  • Hi

    I am using VSTO (via VS201) with Word 2007 (I have tried to get to the Vs2010 forum but keep ending up here but the question is just as relevant if using Vs2008).

    I am adding a rich text content control and then inside I am inserting a table but when I do this it adds a line underneath the table and inside the content control. this means my users can click inside this line and add more data to the content control. I only want them to be able to touch the table. It also creates a line a the top of the table but I can remove this, I have used the following code:-

    // Add Control to document
                            Word.Range range = this.Application.Selection.Range;
                            richTextCtrl = vstoDocument.Controls.AddRichTextContentControl(range, name);
                            richTextCtrl.Title = "UnityTable";
                            richTextCtrl.Tag = tempTable.UnityObjectGuid.ToString();
    
                            // Add table to control                
                            range = this.Application.Selection.Range;
                            Word.Table table = vstoDocument.Tables.Add(range, 2, 2, ref missing, ref missing);
                            table.Borders.Enable = 1;
                            this.Application.Selection.MoveUp(ref missing, ref missing, ref missing);
                            this.Application.Selection.Delete(ref missing, ref missing);
    Is there a way I can stop the default of adding the new line under the table inside the content control, i dont mind what the user does outside of the control.

    Is tried using the SelectionChange event from the VSToobject to see where the user was on the document and then decide if the control should allow editing or not:-

    nativeDocument = this.Application.ActiveDocument;
    vstoDocument = Globals.Factory.GetVstoObject(nativeDocument);
    vstoDocument.SelectionChange += new SelectionEventHandler(vstoDocument_SelectionChange);
    
    
    
            private void vstoDocument_SelectionChange(object sender, Microsoft.Office.Tools.Word.SelectionEventArgs e)
            {
                Word.Selection sel = e.Selection;
                Word.Range rng = sel.Range;
                if (rng.ParentContentControl != null && rng.ParentContentControl.Title != null)
                {
                     // disable control editing if not inside the table
                }
    
            }
    The problem is my range, despite being inside the content control is not a descendent of the content control, the ParentContentControl property is null, it is i think a direct descendent of document. Another problem is after a few times of the event firing it no longer fires anymore so not going to be any use to me as it stands.

    I tried the ContentControlOnEnter event also but if the user was to click on the table to enter the control they could then click the spare line below because the on enter event has already fired, they are already in the control.

    Has anybody had this same problem and managed to remove the extra space/line.

    Thanks

    Paul
    Tuesday, January 05, 2010 4:28 PM

Answers

  • Hi Paul

    The thing you're running into here is that Word stores information in that following paragraph for the table management. IOW, it's "protected" - you aren't allowed to delete it. And given how Content Controls are implemented, there's no way to move it outside the content control.

    About the only thing I can suggest would be to apply editing protection to such paragraphs, the protect the document. (In the Developer tab, use the "Protect Document" button to display the relevant task pane. Use Section 2 in that task pane. Select all things the user should be allowed to edit and check "Everyone". Then activate the protection.)

    Another possibility might be to format it as "hidden" and/or with a very small font. If the user can't see it, he won't click on it. If he discovers it by accident, anything he types he won't be able to see (nor will anyone else).
    Cindy Meister, VSTO/Word MVP
    • Marked as answer by syvers Wednesday, January 06, 2010 1:00 PM
    Tuesday, January 05, 2010 6:30 PM

All replies

  • Hi Paul

    The thing you're running into here is that Word stores information in that following paragraph for the table management. IOW, it's "protected" - you aren't allowed to delete it. And given how Content Controls are implemented, there's no way to move it outside the content control.

    About the only thing I can suggest would be to apply editing protection to such paragraphs, the protect the document. (In the Developer tab, use the "Protect Document" button to display the relevant task pane. Use Section 2 in that task pane. Select all things the user should be allowed to edit and check "Everyone". Then activate the protection.)

    Another possibility might be to format it as "hidden" and/or with a very small font. If the user can't see it, he won't click on it. If he discovers it by accident, anything he types he won't be able to see (nor will anyone else).
    Cindy Meister, VSTO/Word MVP
    • Marked as answer by syvers Wednesday, January 06, 2010 1:00 PM
    Tuesday, January 05, 2010 6:30 PM
  • Hi Cindy,

    Thanks for the info. I am not going to use the rich text content control for the tables I would have been inserting. Instead I will use a normal table straight in the document and will put a plaincontentcontrol in the first cell that will enable me to locate the tables i need to, give me my metadata, and will give my users a better display to work from.

    Once again thanks for your help.

    Paul

    Wednesday, January 06, 2010 12:59 PM