none
How to insert a row after a current row in a table via VBA/VB6 automation

    Question

  • I have a table where a row is identified by a bookmark positioned in a cell.

     

    Set oRange = oDoc.Bookmarks("FirstOperation").range

    I was using,

     

     

    Call oRange.Rows.add

     

    But this adds the row at the end of the table, I need to insert it after the row identified by the bookmark.

     



    • Edited by pcmd_admin Thursday, October 27, 2011 2:53 PM
    Thursday, October 27, 2011 2:50 PM

Answers

  • My original code was basically as per Stefans example. I tried doing a collapse as suggested by Rich but no difference. 
    So I'm back to using a selection which seems to work. Currently trying on my development platform which is Word 2003, but
    this is also deployed to both 2007 & 2010 as well.

    Example doc template: http://pcmd.myzen.co.uk/AJW/SwitchingList.dot



    Dim arrOpTables(1) As Word.Table
    Dim arrOpStartRow(1) As Integer
    Dim arrOpNoColumn(1) As Integer
    Dim oTable As Word.Table
    Dim oRange As Word.range

    'Look for bookmark in first table Set oRange = .Bookmarks("FirstOperation").range 'If bookmark not found, then have to assume the first table If oRange Is Nothing Then Set arrOpTables(0) = .Tables(1) arrOpStartRow(0) = .Tables(1).Rows.Count arrOpNoColumn(0) = 1 For intRowIndex = 1 To (myForm.iGrid1.RowCount * (intBlankRow + 1)) Call .Tables(1).Rows.add Next Else Set arrOpTables(0) = oRange.Tables(1) arrOpStartRow(0) = oRange.Cells(1).RowIndex arrOpNoColumn(0) = oRange.Cells(1).ColumnIndex Call arrOpTables(0).Rows(arrOpStartRow(0)).Select Call oWord.Selection.InsertRows(myForm.iGrid1.RowCount * (intBlankRow + 1)) 'oRange.Collapse (wdCollapseStart) ' 'For intRowIndex = 1 To (myForm.iGrid1.RowCount * (intBlankRow + 1)) ' Call oRange.Rows.add 'Next End If

    • Edited by Andy-W (UK) Monday, October 31, 2011 8:20 AM link to .dot
    • Marked as answer by Bruce Song Friday, November 04, 2011 8:41 AM
    Monday, October 31, 2011 8:11 AM

All replies

  • Try the following:
     
    Dim oRange As Range
    Set oRange = oDoc.Bookmarks("FirstOperation").Range
     
    If oRange.Tables.Count > 0 Then
    oRange.Rows.Add
    End If

    Stefan Blom, Microsoft Word MVP
    Thursday, October 27, 2011 5:53 PM
  • If my table has 4 rows

    1

    2

    3

    4

     

    I have a bookmark in cell 1 for row 2.

    If I then use oRange.Rows, It adds the new rows after row 3

     

     

    Friday, October 28, 2011 6:51 AM
  • Change to use a selection, which also allowed me to specify the number of rows to add

     

     oRange.Select
                   
     Selection.InsertRows (12)

    Friday, October 28, 2011 8:10 AM
  • "andy11252" wrote in message news:9e2c8175-c71c-4c40-b2fa-008435c93cbb@communitybridge.codeplex.com...
     
    Change to use a selection, which also allowed me to specify the number of rows to add
     
     
     
    oRange.Select
                   
    Selection.InsertRows (12)
     
     
    Hmm, the code I suggested certainly works for me, using Word 2010. Maybe I misunderstood what you were trying to do?
     
    Anyway, you shouldn’t have to make use of the selection; it should work anyway.

    Stefan Blom, Microsoft Word MVP
    Friday, October 28, 2011 8:35 AM
  • Hmm,

    Table is 6 rows by 5 columns,

    Row 1

    Row 2    (Columns 2 thro 5 merged in this row)

    Row 3    (Bookmark in column 2)

    Row 4    (Columns 2 thro 5 merged in this row)

    Row 5

    Row 6

    So I set my range to the bookmark, giving me Row 2, Column 2

    I then need to insert some rows after "Row 3" (picking up the cell layout of that row)

    I was doing,

    Set oRange = oDoc.Bookmarks("FirstOperation").Range

    for i = 1 to 5

        oRange.Rows.Add

    next

    This added 5 rows after Row 4 and not Row 3.

     

    Friday, October 28, 2011 9:49 AM
  • Does your bookmark actually just mark the range of text in cell 2 of row 2? If it's marking the entire column 2 then I would expect it to act as it is and insert the new row at the end of the table.

    Try collapsing the range to the start point of the range and then do your Rows.Add. So it would look like:

    Set oRange = oDoc.Bookmarks("FirstOperation").Range

    oRange.Collapse wdCollapseStart

    for i = 1 to 5

        oRange.Rows.Add

    next

     


    Kind Regards, Rich ... http://greatcirclelearning.com
    Friday, October 28, 2011 1:05 PM
  • "Rich Michaels" wrote in message news:31e6e861-5e3f-42f9-819e-caa44e45828c@communitybridge.codeplex.com...
     
    Does your bookmark actually just mark the range of text in cell 2 of row 2? If it's marking the entire column 2 then I would expect it to act as it is and insert the new row at the end of the table.
     
    Try collapsing the range to the start point of the range and then do your Rows.Add. So it would look like:
     
    Set oRange = oDoc.Bookmarks("FirstOperation").Range
     
    oRange.Collapse wdCollapseStart
     
    for i = 1 to 5
     
        oRange.Rows.Add
     
    next
     
     
    Also, you could work with a “copy” (duplicate) of a range object; then there is no chance that it could affect the parent object.

    Stefan Blom, Microsoft Word MVP
    Friday, October 28, 2011 1:39 PM
  • Its just a bookmark inserted after the text in a cell. The user inserts a bookmark in the cell, this identifies the column in the table in which i am to insert data.

     

    ps. my user id has changed (andy11252)

    • Edited by Andy-W (UK) Friday, October 28, 2011 2:43 PM
    Friday, October 28, 2011 2:42 PM
  • Hi Andy,

    I don't see confirmation that you have tried anything that Stefan or myself have suggested for you. Please confirm.

    What is the version of Word that you are using and could you post the actual code you are running?


    Kind Regards, Rich ... http://greatcirclelearning.com
    Friday, October 28, 2011 2:56 PM
  • My original code was basically as per Stefans example. I tried doing a collapse as suggested by Rich but no difference. 
    So I'm back to using a selection which seems to work. Currently trying on my development platform which is Word 2003, but
    this is also deployed to both 2007 & 2010 as well.

    Example doc template: http://pcmd.myzen.co.uk/AJW/SwitchingList.dot



    Dim arrOpTables(1) As Word.Table
    Dim arrOpStartRow(1) As Integer
    Dim arrOpNoColumn(1) As Integer
    Dim oTable As Word.Table
    Dim oRange As Word.range

    'Look for bookmark in first table Set oRange = .Bookmarks("FirstOperation").range 'If bookmark not found, then have to assume the first table If oRange Is Nothing Then Set arrOpTables(0) = .Tables(1) arrOpStartRow(0) = .Tables(1).Rows.Count arrOpNoColumn(0) = 1 For intRowIndex = 1 To (myForm.iGrid1.RowCount * (intBlankRow + 1)) Call .Tables(1).Rows.add Next Else Set arrOpTables(0) = oRange.Tables(1) arrOpStartRow(0) = oRange.Cells(1).RowIndex arrOpNoColumn(0) = oRange.Cells(1).ColumnIndex Call arrOpTables(0).Rows(arrOpStartRow(0)).Select Call oWord.Selection.InsertRows(myForm.iGrid1.RowCount * (intBlankRow + 1)) 'oRange.Collapse (wdCollapseStart) ' 'For intRowIndex = 1 To (myForm.iGrid1.RowCount * (intBlankRow + 1)) ' Call oRange.Rows.add 'Next End If

    • Edited by Andy-W (UK) Monday, October 31, 2011 8:20 AM link to .dot
    • Marked as answer by Bruce Song Friday, November 04, 2011 8:41 AM
    Monday, October 31, 2011 8:11 AM