none
Use VBA to add just a single column to a table in word RRS feed

  • Question

  • I have a few tables in Word, the number of columns and rows is varying, but never more then 15 rows and 6 columns. Some of the cells can be merged horizontally or vertically, so I cannot select a row or column (I get the message that that is not possible in tables with merged cells).

    What I want to do: use a macro to select the last column and add one (no more) column to the right of the table. If I do this using the menu commands, Word does insert just one column, but when I go to the last column or the last cell, using ActiveDocument.Table(index).Columns.Add, VBA does not add a single column, but it doubles the columns present in the table (so in a table of 4 columns, I get 4 new columns). The same happens when I use the insertColumns or the InsertColumnsRight.

    How can I add just one single column to the right of the last columns of my table using a macro? 

    Thursday, November 12, 2015 1:40 PM

Answers

  • Hi Han

    The following works for me, based on your repro steps. Note that you need to explicitly choose a cell that has the column width you want, or specify a column width if you have a particular requirement.

    Sub Insert1Col()
        Dim tbl As word.Table
        Dim nrrows As Long, i As Long
        Dim celWidth As Double
        Dim cel As word.Cell, rw As word.Row
        
        Set tbl = ActiveDocument.Tables(3)
        celWidth = tbl.Cell(1, 1).width
        nrrows = tbl.Rows.Count
        
        For i = 1 To nrrows
            Set rw = tbl.Rows(i)
            nrCols = rw.Cells.Count
            rw.Cells(nrCols).Select
            Selection.InsertCells False
            Selection.MoveStart wdCell, 1
            Selection.Cells(1).width = celWidth
        Next
        
    End Sub
    


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    • Marked as answer by Han Rehm Monday, November 16, 2015 6:50 AM
    Friday, November 13, 2015 5:50 PM
    Moderator
  • You are completely right, I did not mention vertically merged cells. No problem, I don't have them yet. In the future, however, it could be possible that someone starts using them.

    I never worked with the WordOpenXML yet, but I think that will be the best option in the end. I was very surprised to see that the menu command has a different result then the macro, and because of that, I thought at first that I was overlooking some kind of parameter.

    Thanks again for your help.

    • Marked as answer by Han Rehm Tuesday, November 17, 2015 6:50 AM
    Tuesday, November 17, 2015 6:50 AM

All replies

  • Please provide repro steps: the table structure where you're seeing the issue. Tell us number of rows, number of columns and which cells are merged.

    Have you tried recording a macro for the steps ("...using the men commands...") that do work for you?


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    • Marked as answer by Han Rehm Friday, November 13, 2015 7:39 AM
    • Unmarked as answer by Han Rehm Friday, November 13, 2015 7:42 AM
    Thursday, November 12, 2015 4:29 PM
    Moderator
  • Hi Cindy,

    Good to hear from you again.

    To start with your second question: I tried two options: selecting the last column  and adding a column to the right, using the menu. I get just one column. When I, however record this action and replay the macro, I get (in my example) 4 columns to the right.

    I tried the same, selecting only the last cell, but again when I replay the macro, I get 4 columns to the right, and through the menu-command, only one column is added.

    In fact, the problem occurs in all tables where I have merged cells. You can replay this quite simple: open a blank document, insert a table of 3 high and 3 wide, and merge the 3 cells of any row. Now, select the last cell of a non-merged row and press Insert right (in the Rows & Columns group from the Layout tab in the Table Tools menu). Just what I need: one column with one cell to each row is added. Now record this action as a macro, and run the macro: one column is added to the table, with extra cells on the row that has been merged. If you put your cursor in the merged row, and then run the macro, you'll even get three columns added to the table.

    Friday, November 13, 2015 8:15 AM
  • Hi Han

    The following works for me, based on your repro steps. Note that you need to explicitly choose a cell that has the column width you want, or specify a column width if you have a particular requirement.

    Sub Insert1Col()
        Dim tbl As word.Table
        Dim nrrows As Long, i As Long
        Dim celWidth As Double
        Dim cel As word.Cell, rw As word.Row
        
        Set tbl = ActiveDocument.Tables(3)
        celWidth = tbl.Cell(1, 1).width
        nrrows = tbl.Rows.Count
        
        For i = 1 To nrrows
            Set rw = tbl.Rows(i)
            nrCols = rw.Cells.Count
            rw.Cells(nrCols).Select
            Selection.InsertCells False
            Selection.MoveStart wdCell, 1
            Selection.Cells(1).width = celWidth
        Next
        
    End Sub
    


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    • Marked as answer by Han Rehm Monday, November 16, 2015 6:50 AM
    Friday, November 13, 2015 5:50 PM
    Moderator
  • Thank you Cindy,

    This works with only horizontally merged cells in the table. As soon as the table has vertically merged cells, the line "set nw = tbl.Rows(i)" produces an error message (cannot select rows in a table with vertically merged cells).

    Up until now, I have no tables with vertically merged cells, so your solution will work, but I'll have to keep on searching for a solution when cells have been merged both ways.

    Monday, November 16, 2015 6:50 AM
  • Yes, well, when you're asked for repro steps please provide steps for the problem you want to address, not just part of it...

    FWIW there are some combinations that are simply not possible using the Word object model. When you reach that point, you either have to export the document to HTML and modify the HTML, then open the HTML in Word. OR you have to work with the WordOpenXML in memory and write it back to replace the table with the newly structured one.


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Monday, November 16, 2015 4:54 PM
    Moderator
  • You are completely right, I did not mention vertically merged cells. No problem, I don't have them yet. In the future, however, it could be possible that someone starts using them.

    I never worked with the WordOpenXML yet, but I think that will be the best option in the end. I was very surprised to see that the menu command has a different result then the macro, and because of that, I thought at first that I was overlooking some kind of parameter.

    Thanks again for your help.

    • Marked as answer by Han Rehm Tuesday, November 17, 2015 6:50 AM
    Tuesday, November 17, 2015 6:50 AM