none
Microsoft.Office.Interop.Word Building multiple tables RRS feed

  • Question

  • I am using the Microsoft.Office.Interop.Word libraries to build some tables in a Word document. They need to go in a particular section and I am trying to use bookmarks and ranges. But I am having an issue in building multiple tables one after the other. When building a new table you need to send something like:

     

     Dim NewTable As Word.Table = WordApp.ActiveDocument.Tables.Add(range, lNumRows, lNumCols)

     

    This is fine for the first table. But how do I specify that the next table should start just after the previous one? I have tried things such as:

     

    range.SetRange(range.End+1, range.End+1) but it always builds the new table inside the existing one, or just adds a new set of rows to the existing table.

    I also tried using a bookmark which was at the end of the section I wanted to add the tables to and then setting the range by

     

    range.SetRange(range.Start-1, range.Start-1)

     

    But had the same issues. I'm sure it is very simple and I will kick myself once I know how to do it, but please someone help!

     

    Thanks,

    GJ


    • Edited by gj99 Tuesday, November 15, 2011 10:46 AM
    • Moved by Allen_MSDN Wednesday, November 16, 2011 3:36 AM (From:ADO.NET Managed Providers)
    Tuesday, November 15, 2011 10:39 AM

Answers

  • Hi Gj

    So, you have a table object - NewTable. That object has a Range: NewTable.Range. What you need to do is set that to a Range object, then collapse the Range. (Think of it like selecting the table in the UI, then pressing Right-Arrow to have an insertion point at the end of where the selection was.) You also need to have a paragraph separating the two tables, otherwise Word will want to combine them into one table. So the code would go something like this:

    Dim NewTable As Word.Table = WordApp.ActiveDocument.Tables.Add(range, lNumRows, lNumCols)
    Dim tblRange as Word.Range = NewTable.Range
    tblRange.InsertAfter("vbCr") 'add the paragraph
    tblRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
    Dim NextTable as Word.Table = WordApp.ActiveDocument.Tables.Add(tblRange, lNumRows, lNumCols)


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bruce Song Friday, November 25, 2011 1:05 PM
    Wednesday, November 16, 2011 8:36 AM
    Moderator

All replies

  • Hi gj99,

    Regard to your issue, I have moved this case to Word For Developer, you can get more help there.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Wednesday, November 16, 2011 3:38 AM
  • Hi Gj

    So, you have a table object - NewTable. That object has a Range: NewTable.Range. What you need to do is set that to a Range object, then collapse the Range. (Think of it like selecting the table in the UI, then pressing Right-Arrow to have an insertion point at the end of where the selection was.) You also need to have a paragraph separating the two tables, otherwise Word will want to combine them into one table. So the code would go something like this:

    Dim NewTable As Word.Table = WordApp.ActiveDocument.Tables.Add(range, lNumRows, lNumCols)
    Dim tblRange as Word.Range = NewTable.Range
    tblRange.InsertAfter("vbCr") 'add the paragraph
    tblRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
    Dim NextTable as Word.Table = WordApp.ActiveDocument.Tables.Add(tblRange, lNumRows, lNumCols)


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bruce Song Friday, November 25, 2011 1:05 PM
    Wednesday, November 16, 2011 8:36 AM
    Moderator