Solved

Add a row to a Word Table

Posted on 2016-09-06
23
55 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 32

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
 
LVL 31

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

864 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now