Share via

VBA - adding rows to MS Word table

Anonymous
2016-04-08T19:12:36+00:00

I have created a four column table in MS-Word.  The formatting on each row includes a drop down menu in each cell.  A user can insert a row, however the formatting (i.e. dropdown menus)  are not copied to the new row.  

Please provide vba language that would copy the formatting and cells. The formatting would be the default selections in the row, not the selections the user has chosen in the prior row.  Thanks.

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
2016-04-08T22:54:36+00:00

If you are using Legacy FormFields with a document that is Protected for Filling in Forms, you can use the following code in a macro that is run on exit from the last formfield in the last row of the table:

Dim i As Long

If MsgBox("Do you want to add another row?", vbYesNo + vbQuestion) = vbYes Then

    ActiveDocument.Unprotect

    With Selection.Tables(1)

        .Rows(.Rows.count).Range.Copy

        .Rows.Add

        .Rows(.Rows.count).Range.Paste

        .Rows(.Rows.count).Delete

    End With

    ActiveDocument.Protect wdAllowOnlyFormFields, NoReset

    With Selection.Tables(1)

        With .Rows(.Rows.count)

            For i = 1 To 4

                .Cells(i).Range.FormFields(1).DropDown.Value = 1

            Next i

        End With

    End With

End If

Was this answer helpful?

0 comments No comments

11 additional answers

Sort by: Most helpful
  1. Paul Edstein 82,861 Reputation points Volunteer Moderator
    2016-04-09T21:00:48+00:00

    Since you're using content controls, did you bother trying the code for those in the links you've already been provided?

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2016-04-09T14:28:44+00:00

    Thanks for your help.  Just to clarify, none of the rows or cells or protected.  

    A simplified version of the table is below.  You will notice that each row has drop down menus.  The issue arises from the addition of rows either at the end of the table or somewhere adjacent to the already existing rows.  If I insert a row either above or below an already existing row, the drop down menus are not copied to the new row.  

    Currently, the drop down menus are set to the default fields.  However, after the user has selected the values for the below four items the rows will have data in them.  So if I copy the row, the copy function will pick up the user selected values as opposed to the default values.  The copy will not pick up the drop down menus. 

    Please provide code that will insert a row (either above or below an existing row) that will include the drop down menus with the default values of "choose an item" or "click here to enter a date."  

    Choose an item. Choose an item. Click here to enter a date.
    Choose an item. Choose an item. Click here to enter a date.
    Choose an item. Choose an item. Click here to enter a date.
    Choose an item. Choose an item. Click here to enter a date.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2016-04-09T06:19:53+00:00

    See also the code at http://www.gmayor.com/word_vba_examples_2.htm under the heading 'An alternative method of adding a row to a protected table'

    Was this answer helpful?

    0 comments No comments