Word VBA Add Table Rows and Duplicate Tables

Anonymous
2018-07-07T03:57:57+00:00

Hi,

My work is switching from daily case notes to monthly case notes. They are currently in a table... I was previously using forms and bookmarks to automate them, but that is not going to work now that I will have to have duplicated tables in one doc.

Each day will need to be its own table (I think). I will potentially need to add single or multiple rows in the middle of those tables depending on the day's activities.  Each of those rows has its own form content (legacy or activex, it doesn't really matter to me) that needs to copy over when the rows AND when the table copies. The best way to do this would be with buttons, because many people who have very little computer literacy will be using it (the type of people who are confused by keyboard shortcodes).

Since I don't see away to attach a sample, I will include a screenshot:

On the left hand side is what a blank table looks like. On the right hand side is an example of what one day *could* look like.

1/Green - Each Activity can have multiple times

2/Blue - There could be three activites (each having multiple times) per day. Each activity will have its own daily time summary (2a) and Activity Description (2b)

3/Red - Is the entire day summary from date to electronic signature.

I can get the table to add a row, I do not know how to add a button that adds a row to a specific table when there are multiple (unknown, not yet created) tables in a document. And I do not know how to copy everything, including the buttons and potentially bookmarks, to a new table and have it work for only that specific table.

I have the code to protect and unprotect the document during changes through a call routine, but otherwise it will likely be protected. See: computer illiterate people using it.

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
Paul Edstein 82,871 Reputation points Volunteer Moderator
2018-07-09T02:49:46+00:00

For code to add a new row to any table in a document with content controls try:

Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)

'The following code conditionally adds a new row, with content controls, to the current table.

Dim i As Long, j As Long, r As Long, Prot As Variant

Const Pwd As String = "" 'Insert password (if any) here

'Exit if we're not in a table.

If CCtrl.Range.Information(wdWithInTable) = False Then Exit Sub

With CCtrl

  i = .Range.Tables(1).Range.ContentControls.Count

  'Get our ContentControl's index # in the table

  j = ActiveDocument.Range(.Range.Tables(1).Range.Start, .Range.End).ContentControls.Count

  'Check that we're using the last content control

  If i <> j Then Exit Sub

End With

'Solicit user input

If MsgBox("Add new row?", vbQuestion + vbYesNo) <> vbYes Then Exit Sub

With ActiveDocument

  ' Un-protect the document, if applicable

  Prot = .ProtectionType

  If .ProtectionType <> wdNoProtection Then

    Prot = .ProtectionType

    .Unprotect Password:=Pwd

  End If

  With CCtrl.Range

    r = .Cells(1).RowIndex

    With .Tables(1)

      If r = .Rows.Count Then

        'Insert an empty paragraph after our table, then replace it with a replica of the last row

        .Rows.Last.Range.Next.InsertBefore vbCr

      Else

        .Split .Rows(r + 1)

      End If

      .Rows.Last.Range.Next.FormattedText = .Rows(r).Range.FormattedText

      'Reset all content controls in the new row

      For Each CCtrl In .Rows(r + 1).Range.ContentControls

        With CCtrl

          If .Type = wdContentControlCheckBox Then .Checked = False

          If .Type = wdContentControlRichText Or .Type = wdContentControlText Then .Range.Text = ""

          If .Type = wdContentControlDropdownList Then .DropdownListEntries(1).Select

          If .Type = wdContentControlComboBox Then .DropdownListEntries(1).Select

          If .Type = wdContentControlDate Then .Range.Text = ""

        End With

      Next

    End With

  End With

  ' Re-protect the document, if applicable

  .Protect Type:=Prot, Password:=Pwd

End With

End Sub

With the above code, you'll be prompted as to whether you want to add a new row when you exit the last content control in a given table. The new row, if added, will have all content controls cleared.

Was this answer helpful?

0 comments No comments

7 additional answers

Sort by: Most helpful
  1. Doug Robbins - MVP - Office Apps and Services 323.8K Reputation points MVP Volunteer Moderator
    2018-07-07T23:19:27+00:00

    When you need to add, or insert a row, exactly what is it that you want to add or insert.  I am assuming that it is not just an empty row.

    Do you want to be able to Add or Insert

    1. a Daily Time Summary (3) or
    2. an "Activity" block (2b), or
    3. an additional row in the start and finish times and duration section (1)

    or, at different times, do you want to be able to add or insert any one of the above?

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2018-07-07T16:49:40+00:00

    Doug's suggestion of using Selection.Tables(1).Rows.Add works from where the current selection point is in the table. In other words, the selection point would have to be in a table and not someplace else in the document. The Selection method property of "Tables(1)" in this case does not mean the first table in the collection of tables in the document.

    To make the VBA more robust and allow for the scenario of the selection including more than one table, there would need to be additional conditional logic.

    If Selection.Tables.Count = 0 then

      'return a message that the selection point is not in a table

    ElseIF Selection.Tables.Count > 1

      'return a message asking for clarification on what table to add the row

    Else

      Selection.Table(1).Rows.Add

    End If

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2018-07-07T15:39:13+00:00

    I guess my confusion comes is that I do not know how to tie a macro to a specific table when I don't know what the table name will be (since I don't know if I am going to have 2 case notes/tables or 30 for the month). How do I tell it to add a row to table 29 and not table 1. We also don't necessarily work in order (we write them after the fact when we are in the field and during when we aren't), so I may work on 7/7 and 7/6 before I work on 7/2, so I may need to add a row to a table that may not be at the bottom. I also don't know how to add a row in the right place in the table because I cannot predict how many times I may need per activity (1/green below), and then how many activities (2/blue) per table until I am writing the case notes. I don't know if that makes sense at all.

    (See explanation in my first post to understand the different parts in the screenshots below. Everything in the table is on its own row, I just deleted borders to make it more streamlined.)

    Was this answer helpful?

    0 comments No comments
  4. Doug Robbins - MVP - Office Apps and Services 323.8K Reputation points MVP Volunteer Moderator
    2018-07-07T08:33:34+00:00

    To include a screen shot in your post, you need to use the Insert Image button

    Though it may be better to upload a document to a folder on your OneDrive and set that FOLDER to be shared and paste a link to it into a message that you post back here.

    To add a row to the table in which the selection is location, you would use the following VBA command

    Selection.Tables(1).Rows.Add

    I would suggest that you should be creating a template that contains a customized ribbon with a custom tab that contains buttons that will carry out the commands that you need to use as you probably do not really want buttons in the body of the document.

    Was this answer helpful?

    0 comments No comments