locked
Automating Word - Table Within a Table RRS feed

  • Question

  • Using VB.Net to create Word 2003 document (Late Binding)

    I'm trying to create an inner table in the first cell of an outer table.

    I tried this:
    Dim oRange_Outer = m_oDoc.Bookmarks.Item("\endofdoc").Range
    Dim oTable_Outer = m_oDoc.Tables.Add(oRange_Outer, 1, 3)
    
    nRows = 5
    nCols = 6
    oRange = oTable_Outer.Cell(1, 1).Range
    oTable = m_oDoc.Tables.Add(oRange, nRows, nCols) 
    
    

    Following the code above, these lines seem to work without failure:
    oTable.Columns(1).Width = m_oWord.InchesToPoints(0.166)
    oTable.Columns(2).Width = m_oWord.InchesToPoints(0.166)
    oTable.Columns(3).Width = m_oWord.InchesToPoints(0.166)
    oTable.Columns(4).Width = m_oWord.InchesToPoints(0.166)
    oTable.Columns(5).Width = m_oWord.InchesToPoints(0.166)
    oTable.Columns(6).Width = m_oWord.InchesToPoints(0.166)
    
    oTable.Rows(1).Height = m_oWord.InchesToPoints(2) 
    
    

    But it fails here:
    oTable.Rows(2).Height = m_oWord.InchesToPoints(2) 
    


    The error reported is:
    "The requested member of the collection does not exist."

    Any ideas?

    Yes, I know I can record a macro in Word, but every time I try that, the Word macro makes reference to "Selection"  which has no meaning in my code.


    Clif
    Wednesday, November 16, 2011 8:22 PM

Answers

  • Hi Clif,

    Welcome to the MSDN Forum.

    When you create a table, by default, there are only one row, so if you don't inistallize it, the other rows don't exist, so I write a space value to each cell. It works now. Here is the whole test code:

        Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
            Dim oWord As Word.Application
            Dim oDoc As Word.Document
            Dim oTable1 As Word.Table
            Dim oTable2 As Word.Table
            'Start Word and open the document template.
            oWord = New Word.Application
            oWord.Visible = True
            oDoc = oWord.Documents.Add
            GC.Collect()
            GC.WaitForFullGCComplete()
            'Insert a 1 x 3 table, fill it with space data.
            Dim nRows As Integer, nCols As Integer
            oTable1 = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, 1, 3)
            oTable1.Range.ParagraphFormat.SpaceAfter = 6
            For r = 1 To 1
                For c = 1 To 3
                    oTable1.Cell(r, c).Range.Text = " "
                Next
            Next
            oTable1.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle
            oTable1.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle
            nRows = 5
            nCols = 6
            Dim oRange As Word.Range = oTable1.Cell(1, 1).Range
            oTable2 = oDoc.Tables.Add(oRange, nRows, nCols)
            oTable2.Range.ParagraphFormat.SpaceAfter = 6
            For r = 1 To nRows
                For c = 1 To nCols
                    oTable2.Cell(r, c).Range.Text = " "
                Next
            Next
            oTable2.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle
            oTable2.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle
            oTable2.Rows(2).Height = oWord.InchesToPoints(4)
            oDoc.Save()
            oWord.Quit()
        End Sub
    


    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Clif001 Monday, November 21, 2011 1:20 PM
    Thursday, November 17, 2011 6:22 AM
    Moderator

All replies

  • Hi Clif,

    Welcome to the MSDN Forum.

    When you create a table, by default, there are only one row, so if you don't inistallize it, the other rows don't exist, so I write a space value to each cell. It works now. Here is the whole test code:

        Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
            Dim oWord As Word.Application
            Dim oDoc As Word.Document
            Dim oTable1 As Word.Table
            Dim oTable2 As Word.Table
            'Start Word and open the document template.
            oWord = New Word.Application
            oWord.Visible = True
            oDoc = oWord.Documents.Add
            GC.Collect()
            GC.WaitForFullGCComplete()
            'Insert a 1 x 3 table, fill it with space data.
            Dim nRows As Integer, nCols As Integer
            oTable1 = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, 1, 3)
            oTable1.Range.ParagraphFormat.SpaceAfter = 6
            For r = 1 To 1
                For c = 1 To 3
                    oTable1.Cell(r, c).Range.Text = " "
                Next
            Next
            oTable1.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle
            oTable1.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle
            nRows = 5
            nCols = 6
            Dim oRange As Word.Range = oTable1.Cell(1, 1).Range
            oTable2 = oDoc.Tables.Add(oRange, nRows, nCols)
            oTable2.Range.ParagraphFormat.SpaceAfter = 6
            For r = 1 To nRows
                For c = 1 To nCols
                    oTable2.Cell(r, c).Range.Text = " "
                Next
            Next
            oTable2.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle
            oTable2.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle
            oTable2.Rows(2).Height = oWord.InchesToPoints(4)
            oDoc.Save()
            oWord.Quit()
        End Sub
    


    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Clif001 Monday, November 21, 2011 1:20 PM
    Thursday, November 17, 2011 6:22 AM
    Moderator
  • Hi Clif,

    Do you have any update?

    How about your issue now? If you need further assistance, please feel free to let me know.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, November 21, 2011 4:27 AM
    Moderator
  • Thanks.  :)

     

     


    Clif
    Monday, November 21, 2011 1:21 PM