PDA

View Full Version : Insert Row in a Table



Loss1003
04-13-2015, 11:39 AM
How should I revise the code to add a specified number rows in a word table.

Once the command button is clicked on the last active row is deselected therefore the code will not run properly. is there a way to capture the last active/selected row before running the code?


'A basic Word macro coded by Greg Maxey


Dim lngIndex As Long, lngRowsToAdd As Long, lngPosit As Long
Dim oTbl As Word.Table
If Selection.Information(wdWithInTable) Then
lngRowsToAdd = InputBox("How many rows?", "Add Rows", 1)
Set oTbl = Selection.Tables(1)
lngPosit = Selection.Rows(1).Range.Information(wdEndOfRangeRowNumber)
For lngIndex = 1 To lngRowsToAdd
oTbl.Rows.Add (oTbl.Rows(lngPosit))
Next lngIndex
End If

gmayor
04-13-2015, 11:29 PM
Presumably this relates to your previous question? If so, you make things difficult by the use of a table with variable formatting and varying numbers of columns, both of which make programming a pain. However the following should do what you want.



Private Sub CommandButton2_Click()
Dim oRow As Row
Dim oNextRow As Row
Dim oPrevRow As Row
Dim lngRow As Long
Dim lngRowsToAdd As Long
Dim lngIndex As Long
Dim oCell As Cell
lngRowsToAdd = InputBox("How many rows?", "Add Rows", 1)
Set oRow = ActiveDocument.Bookmarks("VendComSect").Range.Rows(1)
oRow.Range.Copy
Set oRow = ActiveDocument.Bookmarks("LCRemSect").Range.Rows(1)
lngRow = oRow.Index + 1
oRow.Range.Paste
Set oNextRow = ActiveDocument.Bookmarks("VendComSect").Range.Next.Next.Rows(1)
oNextRow.Range.Copy
For lngIndex = 1 To lngRowsToAdd
oRow.Range.Paste
Set oPrevRow = oRow.Previous.Range.Rows(1)
For Each oCell In oPrevRow.Cells
oCell.Range.Text = ""
oCell.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
If lngIndex > 1 Then
oCell.Borders(wdBorderTop).LineStyle = wdLineStyleNone
End If
Next oCell
Next lngIndex
lbl_Exit:
Set oRow = Nothing
Set oPrevRow = Nothing
Set oNextRow = Nothing
Set oCell = Nothing
Exit Sub
End Sub

Loss1003
04-14-2015, 06:18 AM
Thanks. It does partially relate to my previous questions. However, I wanted the user to be able to highlight any desired row withing the table and then click the command button to insert a specified number of new rows below the selected row.

I've used a code in excel that works great but as I'm learning things in word especially with tables are much different. :)

gmayor
04-14-2015, 06:52 AM
Greg's macro that you quoted earlier almost does what you require. The following minor mod will insert the selected number of rows after the row the cursor is in.


Sub InsertRows()
Dim lngIndex As Long, lngRowsToAdd As Long, lngPosit As Long
Dim oTbl As Word.Table
If Selection.Information(wdWithInTable) Then
lngRowsToAdd = InputBox("How many rows?", "Add Rows", 1)
Set oTbl = Selection.Tables(1)
lngPosit = Selection.Rows(1).Range.Information(wdEndOfRangeRowNumber)
If lngPosit = oTbl.Rows.Count Then
oTbl.Rows.Add
lngRowsToAdd = lngRowsToAdd - 1
End If
lngPosit = lngPosit + 1
For lngIndex = 1 To lngRowsToAdd
oTbl.Rows.Add (oTbl.Rows(lngPosit))
Next lngIndex
End If
lbl_Exit:
Exit Sub
End Sub

Loss1003
04-14-2015, 07:22 AM
Having the same issue as previous when I click on the command button it pulls me outside of the table and deselects the row. therefore, no rows will be added. I've tried moving the button inside the table. However, the rows will only be added below whatever row the button is place.

gmayor
04-14-2015, 10:07 PM
The macro works with the selection. If you use a button on the document, then as soon as you click the button, the selection moves to the button. You can't use a button on the document for this type of action. You would need a button on the ribbon or QAT (Quick Access Toolbar) which doesn't affect the selection; or you would have to specifically tell the macro which row of the table to start from, as in the earlier example.

http://gregmaxey.mvps.org/word_tip_pages/customize_ribbon_main.html

Loss1003
04-15-2015, 07:58 AM
Thanks for the help. I got the code to run adding it to my ribbon.

How can i adjust the code to set the background of any inserted rows to white

.Shading.BackgroundPatternColor = backColor



Dim lngIndex As Long, lngRowsToAdd As Long, lngPosit As Long
Dim oTbl As Word.Table
If Selection.Information(wdWithInTable) Then
lngRowsToAdd = InputBox("How many rows?", "Add Rows", 1)
Set oTbl = Selection.Tables(1)
lngPosit = Selection.Rows(1).Range.Information(wdEndOfRangeRowNumber)
For lngIndex = 1 To lngRowsToAdd
oTbl.Rows.Add (oTbl.Rows(lngPosit))
Next lngIndex
End If

gmayor
04-15-2015, 09:46 PM
Modify as follows:



Sub InsertRows()
Dim lngIndex As Long, lngRowsToAdd As Long, lngPosit As Long
Dim oTbl As Word.Table
Dim oRow As Row
If Selection.Information(wdWithInTable) Then
lngRowsToAdd = InputBox("How many rows?", "Add Rows", 1)
Set oTbl = Selection.Tables(1)
lngPosit = Selection.Rows(1).Range.Information(wdEndOfRangeRowNumber)
If lngPosit = oTbl.Rows.Count Then
Set oRow = oTbl.Rows.Add
oRow.Range.Shading.BackgroundPatternColor = wdColorWhite
lngRowsToAdd = lngRowsToAdd - 1
End If
lngPosit = lngPosit + 1
For lngIndex = 1 To lngRowsToAdd
Set oRow = oTbl.Rows.Add(oTbl.Rows(lngPosit))
oRow.Range.Shading.BackgroundPatternColor = wdColorWhite
Next lngIndex
End If
lbl_Exit:
Exit Sub
End Sub