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

8 answers

Sort by: Most helpful
  1. Anonymous
    2018-07-08T16:44:35+00:00

    Thank you for all of your responses.

    At any given point, I would need to be able to add/insert all of #2, both 2a and 2b (they work in tandem... if I have a new activity, there needs to be time summary for it) and #1 (extra times) to any of the 2s, as any activity potentially can take multiple times. And then I would need to be able to duplicate the whole thing for the next case note, if needed.

    Was this answer helpful?

    0 comments No comments
  2. Doug Robbins - MVP - Office Apps and Services 323.5K 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
  3. 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
  4. 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
  5. Doug Robbins - MVP - Office Apps and Services 323.5K 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