none
Need help placing table in correct location RRS feed

  • Question

  • I have some VB.net code where I create a document and place tables on it. The number of rows a table will have can vary but the row height is always the same so I know the maximum number of rows a table can have to fit on one page. What I want to happen when a table reaches this max number is for another page to be added to the document and a new table be added to that page. It sounds simple enough but I am having all sorts of trouble with this. Probably because of my ignorance when it comes to controlling the cursor position with code.

    I have tried this solution but it did not work.

    TableLocation = TableObject.Range 'get the range object of the current table
    TableLocation.Collapse(wdCollapseEnd) 'move the cursor to the end of the table
    TableLocation.Select() 'select it
    WordDocument.Application.Selection.InsertBreak(wdPageBreak) 'add a new page

    What actually happens is two pages are added instead of one. I've also tried Selection.InsertNewPage and I get the same result.

    I have also tried this solution and it did not work either.

    TableLocation = TableObject.Range 'get the range object of the current table
    TableLocation.Collapse(wdCollapseEnd) 'move the cursor to the end of the table
    TableLocation.Select() 'select it
    WordDocument.Application.Selection.InsertBreak(wdLineBreak) 'add a new line

    Adding the line causes a new page to be added to the document. That's good but, for some reason, two lines are actually added to that new page so when I add my new table, it's not at the beginning of the page. It's one line down.

    If someone could school me on getting tables placed where I need them placed, I would really appreciate it.

    Thanks,

    Darren

    Monday, July 1, 2019 7:50 PM