Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Add a row to a Word Table

Posted on 2016-09-06
23
61 Views
Last Modified: 2016-09-22
I've created a Word 2010 document with a table.  Users will need to add information to the table (text, drop-down menus and date picker). They will need to add a row to the table and copy all of the control boxes.  I would like to add a button that they can click on to add the row and copy all formatting/controls below the selected row.  Can someone help me with this code?
0
Comment
Question by:Tina Robinson
  • 11
  • 7
  • 3
  • +2
23 Comments
 
LVL 33

Expert Comment

by:Rob Henson
ID: 41786347
Hi Tina,

Can you upload a sample file so that we can see the items that will need copying?

When in the last cell of a table (bottom row, far right column) pressing the tab key will create a new row. I know that this copies formatting from the row above; I assume it doesn't copy any of the controls as well?

Thanks
Rob
0
 
LVL 45

Expert Comment

by:aikimark
ID: 41786352
Please post a representative sample document
0
 

Author Comment

by:Tina Robinson
ID: 41786359
I've attached the file.  Under each semester, users will need to add additional rows as needed. I wanted to add a button that they can click and it will add the row with the controls/formatting.  Thank you for any help you can give me!
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 32

Expert Comment

by:Paul Sauvé
ID: 41786450
no file yet!
0
 
LVL 45

Expert Comment

by:aikimark
ID: 41786452
I've attached the file
I don't see any attachment
0
 

Author Comment

by:Tina Robinson
ID: 41786462
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 41786682
This will add another row the same as the last one
Sub AddRow()
    Dim tbl As Table
    Dim rng As Range
    
    Set tbl = ActiveDocument.Tables(2)
    Set rng = tbl.Rows.Last.Range
    rng.Copy
    rng.Collapse wdCollapseEnd
    rng.Paste
End Sub

Open in new window

0
 

Author Comment

by:Tina Robinson
ID: 41786690
Thank you - I'm new at this and am not sure where to add this.  Do I create an Active X control button and add this when I click on View Code?
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 41786692
Oops. I see that you want to copy the selected row.
Sub AddIntermediateRow()
    Dim rw As Row
    Dim rng As Range
    
    Set rw = Selection.Rows.Last
    Set rng = rw.Range
    rng.Copy
    rng.Collapse wdCollapseEnd
    rng.Paste
End Sub

Open in new window

0
 

Author Comment

by:Tina Robinson
ID: 41786702
How do I add this code to the button?
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 41786751
I thought you had that sussed.

There are several ways, but the simplest would be to add a Macrobutton field. First write your macro, then navigate thus:  Insert tab, Text group, Quick Parts button, Field... item. Choose the Macrobutton field and then browse to your macro. Change the 'Displayed text' to whatever you need and click OK.
0
 

Author Comment

by:Tina Robinson
ID: 41786753
I clicked on my control button, view code, add added the above code.  However, when I click on the button (and my cursor is in a table cell) a new row is not added.  What am I doing wrong?  Thanks
0
 

Author Comment

by:Tina Robinson
ID: 41786773
GrahamSkan - I don't understand your instructions. Is there anyway you can tell me how to do this with the code that you provided me?
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 41787420
Open the VBA editor with Alt + F11.

Select the ThisDocument module of your document's project in Project explorer. Select CommandButton1 in the left-hand list box in the code pane. Select the Click event in the right-hand box. This should open an empty event Sub:
Option Explicit

Private Sub CommandButton1_Click()

End Sub

Open in new window


Put the code into that procedure.
0
 

Author Comment

by:Tina Robinson
ID: 41787711
This is what I have:

Private Sub CommandButton1_Click()
Sub AddIntermediateRow()
    Dim rw As Row
    Dim rng As Range
   
    Set rw = Selection.Rows.Last
    Set rng = rw.Range
    rng.Copy
    rng.Collapse wdCollapseEnd
    rng.Paste
End Sub
End Sub

When I click on the button and run it - the first row (Private Sub... is yellow)
0
 
LVL 45

Expert Comment

by:aikimark
ID: 41787779
You can't nest routines like that.  Please change to this:
Private Sub CommandButton1_Click()
    Dim rw As Row
    Dim rng As Range
    
    Set rw = Selection.Rows.Last
    Set rng = rw.Range
    rng.Copy
    rng.Collapse wdCollapseEnd
    rng.Paste
End Sub

Open in new window

0
 

Author Comment

by:Tina Robinson
ID: 41787858
Now when I run the macro (after I click on one of the rows) I get the following error message:

Run time error "5941" - The requested member of the collection does not exist.

 I can't believe this is so difficult. I did it in two minutes in Excel but just can't get it to work in Word.  Thanks again for all of your help!!!
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 41787956
aikimark, thank you.
I didn't realise that my explanation could be read like that.

Tina Robinson
In case the user's selection is not within a table row, try this.
Private Sub CommandButton1_Click()
    Dim rw As Row
    Dim rng As Range
    
    If  Selection.Rows.Count > 0 then
        Set rw = Selection.Rows.Last
        Set rng = rw.Range
        rng.Copy
        rng.Collapse wdCollapseEnd
        rng.Paste
    endif
End Sub

Open in new window

0
 

Author Comment

by:Tina Robinson
ID: 41787976
I just tried that and it's still giving me the same error message.  I think I'm going to give up.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 41788014
Yes I know. I am working on a solution.
0
 

Author Comment

by:Tina Robinson
ID: 41788023
I really do appreciate all of your help!
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 41788054
No that won't work either.

The problem is that when you click on the button, you move the selection from a table row to the button.

A modified macro could ask which row is to be duplicated. However, I think a better solution would be to use a shortcut keystroke and/or add a QAT button to call the macro. Also the macro could be in a code module of the template, instead of the ThisDocument module of the actual document .
If the document is created from a particular template, use that one, otherwise use the Normal template.

Insert a Module using the Insert menu in the VBA IDE and put the code there:
Private Sub AddIntermediateRow()
    Dim rw As Row
    Dim rng As Range
    
    If Selection.Rows.Count > 0 Then
        Set rw = Selection.Rows.Last
        Set rng = rw.Range
        rng.Copy
        rng.Collapse wdCollapseEnd
        rng.Paste
    End If
End Sub

Open in new window

In find Word Options via the File tab or Office button.
Select Customize.
Select Macros in the 'Choose command from' box
Click the Add>> button to add a button to the QAT
Sreparately, you can click the Customise button and navigate to the macro in the Customize keyboard dialogue. You can then specify the shortcut keys to call the macro
0
 

Author Closing Comment

by:Tina Robinson
ID: 41810731
Thank you for all of your help.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question