Solved

Add a row to a Word Table

Posted on 2016-09-06
23
49 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 31

Expert Comment

by:Rob Henson
Comment Utility
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
Comment Utility
Please post a representative sample document
0
 

Author Comment

by:Tina Robinson
Comment Utility
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é
Comment Utility
no file yet!
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
I've attached the file
I don't see any attachment
0
 

Author Comment

by:Tina Robinson
Comment Utility
0
 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
How do I add this code to the button?
0
 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:Tina Robinson
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Yes I know. I am working on a solution.
0
 

Author Comment

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

Accepted Solution

by:
GrahamSkan earned 500 total points
Comment Utility
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
Comment Utility
Thank you for all of your help.
0

Featured Post

A Knowledge Base That Stays Up-to-Date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

There is a feature provided by MS Word that lets you create an Table of Contents for your Word document automatically. To use this feature for other documents there are two steps involved,   1.  Prepare your document for a table of contents (he…
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…
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

771 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

10 Experts available now in Live!

Get 1:1 Help Now