none
How do you add a column to a specific row in a Word table using Visual Studio 2010? RRS feed

  • Question

  • I created a table in a Word document using Visual Basic.  This table contains 4 rows and one long column.  I would like to add an additional column to row #3, so that row #3 contains 2 columns, but the other 3 rows still only contain one column.

    Can this be done?  Or is the only way to do this is to create a separate "table" for row #3 (i.e. a table containing 1 row & 2 columns)? 

    I've tried several different things and I have not been able to get this to work using only one table.  If I create separate tables for rows #1 - 2 (one column), row #3 (two columns), and row #4 (one column), there is an annoying gap between each table, which I do not want.

    Thanks in advance,

    Jim

     

    James Hutchinson

    Monday, January 25, 2016 7:16 PM

Answers

  • There are no NumRows & NumColumns parameters for the Split method in your code...

    You should have:
    wordApp.ActiveDocument.Tables(4).Rows(3).Cells(1).Split(NumRows:=1, NumColumns:=2)


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by jhutch03 Friday, January 29, 2016 10:35 PM
    Friday, January 29, 2016 10:21 PM

All replies

  • Try something along the lines of (VBA):

    ActiveDocument.Tables(1).Rows(3).Cells.Add

    or, if you want to split the existing cell:

    ActiveDocument.Tables(1).Cell(3, 1).Split NumRows:=1, NumColumns:=2


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Tuesday, January 26, 2016 12:47 AM
  • Paul,

    Visual Basic doesn't like the term "ActiveDocument", it causes a syntax error.  Do you know what I can use instead of this?

    Thanks,

    Jim


    James Hutchinson

    Tuesday, January 26, 2016 1:04 AM
  • Instead of 'ActiveDocument', you would use whatever reference you've already created for your document.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Tuesday, January 26, 2016 1:14 AM
  • Hi Jim,

    >> Visual Basic doesn't like the term "ActiveDocument

    Do you do that in an application level add-in, if so, using Application.ActiveDocument.

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, January 26, 2016 7:27 AM
    Moderator
  • Starain,

    Thanks for pointing me in the right direction.  This worked:

    wordApp.ActiveDocument.Tables(4).Rows(3).Cells.Add()

    Unfortunately, now I have a new problem.  How can I readjust the width of my original column and my new column?  My new column is sticking out past the right margin of my document.  

    I would like to be able to readjust the size of both columns in Row #3 so that both columns together are the same width as rows 1, 2, and 4.  Rows 1, 2, and 4 currently contain one long column/cell.

    Thanks,

    Jim   


    James Hutchinson



    • Edited by jhutch03 Thursday, January 28, 2016 12:04 AM mistake
    Thursday, January 28, 2016 12:02 AM
  • I already showed you how to do that, using the Spit method. That method will create two cells, each half the width of the original.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, January 28, 2016 3:07 AM
  • Paul,

    I tried doing this a little while ago, but I still have only one column in Row 3:

    tbl4.Rows(3).Cells(1).Split()

    I also tried this, but I still have only one column in Row 3:

    wordApp.ActiveDocument.Tables(4).Rows(3).Cells(1).Split()

    What am I doing wrong here?

    Thanks,

    Jim


    James Hutchinson

    Friday, January 29, 2016 1:18 AM
  • As indicated in my first reply, the Split method takes two parameters: NumRows & NumColumns. If you don't supply those, nothing happens.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, January 29, 2016 1:25 AM
  • Paul,

    I thought I did do the same thing you did.  Here's my full code for this table:

    'Add the table.
            doc.Tables.Add( _
                Range:=rngTbl3, _
                NumRows:=4, NumColumns:=1)
    
    Dim tbl4 As Word.Table = doc.Tables(4)
            tbl4.Range.Font.Size = 14
            tbl4.Range.Font.Name = "Times New Roman"
            tbl4.Style = "Table Grid"
            tbl4.ApplyStyleFirstColumn = False
            tbl4.ApplyStyleLastColumn = False
            tbl4.ApplyStyleLastRow = False
    
    'tbl4.Rows.Add()
            Dim rngCell7 As Word.Range
            rngCell7 = tbl4.Cell(1, 1).Range
            tbl4.Range.Shading.BackgroundPatternColor = Word.WdColor.wdColorGray05
            tbl4.Range.Text = "Requirements:" & vbCrLf &
            "The organization blah, blah" & vbCrLf &
            "The organization blah, blah."
            tbl4.Range.Font.Size = 12
            tbl4.Range.Font.Bold = vbYes
            tbl4.Range.Font.ColorIndex = Word.WdColorIndex.wdBlack
            tbl4.Rows(1).Cells(1).SetHeight(60.0, Microsoft.Office.Interop.Word.WdRowHeightRule.wdRowHeightAtLeast)
    
    rngCell7.ParagraphFormat.Alignment = _
            Word.WdParagraphAlignment.wdAlignParagraphLeft
    
    
    'Here's where I'm trying to create a new column in Row #3
    wordApp.ActiveDocument.Tables(4).Rows(3).Cells(1).Split()
    


    James Hutchinson


    • Edited by jhutch03 Friday, January 29, 2016 9:10 PM mistake
    Friday, January 29, 2016 9:07 PM
  • There are no NumRows & NumColumns parameters for the Split method in your code...

    You should have:
    wordApp.ActiveDocument.Tables(4).Rows(3).Cells(1).Split(NumRows:=1, NumColumns:=2)


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by jhutch03 Friday, January 29, 2016 10:35 PM
    Friday, January 29, 2016 10:21 PM
  • That worked.  Thank you!

    James Hutchinson

    Friday, January 29, 2016 10:35 PM