none
How to programmatically repeat a group of content controls in Word 2010? RRS feed

  • Question

  • Hi everyone

    I need to be able to programmatically group some content controls on a page in Word 2010 and repeat them as and when I need and be able to set their text to different values in every group.

    I am wondering what is the best way to achieve this requirement.

    Thursday, May 10, 2018 12:37 PM

Answers

  • I managed to fix my problem thanks to macropod who put me in the right direction.

    I found out that if content controls I want to repeat are grouped in a table, I can copy and paste the table anywhere I want and all content controls in the source table are duplicated in the destination table.

    Note that this causes content controls to have the same names/tags, which means that the best way to access them properly is to look for them within their containing table only as opposed to within the document object.

    Below is the code I used

    // Find the already bookmarked source table with content controls
    object bookmarkName = "ContactDetailsTable";
    var bookmark = ThisDocument.Bookmarks[bookmarkName];
    var sourceTable = bookmark.Range.Tables[1];
    
    // Move insertion point down one line
    sourceTable.Range.Select();
    object unit = WdUnits.wdLine;
    object count = 1;
    object extend = WdMovementType.wdMove;
    ThisDocument.ActiveWindow.Selection.MoveDown(ref unit, ref count, ref extend);
    
    // Insert a new line at the current insertion point
    ThisDocument.ActiveWindow.Selection.TypeText("\r\n");
    ThisDocument.ActiveWindow.Selection.TypeParagraph();
    
    // Add a new table and populate it with the content of the source table
    var newTable = ThisDocument.Tables.Add(ThisDocument.ActiveWindow.Selection.Range, 2, 1);
    sourceTable.Range.Copy();
    newTable.Range.Paste();
    
    // Change the text value of the content controls in the new table
    var cc = newTable.Range.ContentControls.get_Item(1);
    cc.Range.Text = "...";
    

    • Marked as answer by seemorgh Thursday, May 17, 2018 3:48 PM
    Thursday, May 17, 2018 3:47 PM

All replies

  • How you go about this depends on how your content controls are grouped. For code to conditionally add a new row with content controls to a table, see:
    http://www.msofficeforums.com/word-vba/27809-code-add-new-row-table.html#post87989
    http://www.msofficeforums.com/word-vba/13955-macro-add-row-table-word-form.html#post38461

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, May 11, 2018 2:09 AM
  • I managed to fix my problem thanks to macropod who put me in the right direction.

    I found out that if content controls I want to repeat are grouped in a table, I can copy and paste the table anywhere I want and all content controls in the source table are duplicated in the destination table.

    Note that this causes content controls to have the same names/tags, which means that the best way to access them properly is to look for them within their containing table only as opposed to within the document object.

    Below is the code I used

    // Find the already bookmarked source table with content controls
    object bookmarkName = "ContactDetailsTable";
    var bookmark = ThisDocument.Bookmarks[bookmarkName];
    var sourceTable = bookmark.Range.Tables[1];
    
    // Move insertion point down one line
    sourceTable.Range.Select();
    object unit = WdUnits.wdLine;
    object count = 1;
    object extend = WdMovementType.wdMove;
    ThisDocument.ActiveWindow.Selection.MoveDown(ref unit, ref count, ref extend);
    
    // Insert a new line at the current insertion point
    ThisDocument.ActiveWindow.Selection.TypeText("\r\n");
    ThisDocument.ActiveWindow.Selection.TypeParagraph();
    
    // Add a new table and populate it with the content of the source table
    var newTable = ThisDocument.Tables.Add(ThisDocument.ActiveWindow.Selection.Range, 2, 1);
    sourceTable.Range.Copy();
    newTable.Range.Paste();
    
    // Change the text value of the content controls in the new table
    var cc = newTable.Range.ContentControls.get_Item(1);
    cc.Range.Text = "...";
    

    • Marked as answer by seemorgh Thursday, May 17, 2018 3:48 PM
    Thursday, May 17, 2018 3:47 PM