0
votes

I am using a VBA macro to insert rows (two columns) in a word document. The problem is the inserted rows don't fill the entire page and all the columns don't have the same width:

enter image description here

My question is: how to give the same width to all the columns and to expand the table to fill the page width?

Here is my function:

Private Function CreateWordDoc(ByVal wrdApp As Word.Application, ByRef Objects() As OwnClass, ByVal sFilename As String, ByVal sPath As String)
    Dim i As Integer
    Dim wrdDoc As Word.Document
    Dim MyObj As OwnClass

    Dim wrdTppTable As Word.Table

    Set wrdDoc = wrdApp.Documents.Add(sFilename, Visible:=True)

    Set wrdTppTable = wrdDoc.Tables(2)

    For i = 0 To UBound(Objects) - 1
        Set MyObj = Objects(i)
        ' Add a row to the table and select it
        wrdTppTable.Rows.Add.Select
        ' Work with the selected row
        With wrdApp.Selection.Range
            ' Make sure the row is on two columns
            .Cells.Split 1, 2, True
            ' Set the text font parameters
            With .Font
                .ColorIndex = wdBlack
                .name = "Arial"
                .size = 11
                .Bold = False
            End With
            ' Write text in the cell
            .Text = MyObj.GetKey & ": " & MyObj.GetValue
            ' Then select the next cell in the row
            .Next.Select
        End With
        ' Work with the second column of the row
        wrdApp.Selection.Cells.SetWidth 54, RulerStyle:=wdAdjustFirstColumn
        With wrdApp.Selection.Range
            With .Font
                .ColorIndex = wdBlack
                .name = "Arial"
                .size = 11
                .Bold = False
            End With
            ' Write the cell
            .Text = MyObj.GetId
        End With
    Next
End Function
1
You can set the width of the column by affecting this property directly (.Columns(x).Width). On the other hand, I don't have too clear various parts of your code; for example: why are you splitting over and over, instead of creating the number of columns you wish (an, evetually, split in a specific row)?varocarbas
I'm quite new to VBA with Word. I'm not sure the splitting is the best way of doing this. I'm open any code proposal. If you have one, I would be glad to see it. Feel free to answer and to post some code ;) For information, I add rows to a word template and the template already contains a table with one row (header) but with only one column. All other rows must have two columns.Maxbester
Makes sense :) I have written an answer with the required code to fix your resizing problems; remove all the splits an setWidths from your code and just write this on the top.varocarbas
PS: your code is pretty confusing in other parts too (what is Objects?!); but I guess that it works as you want and that your whole problem was the column resizing part.varocarbas
Yes Objects is an array of objects I defined in my code. This is not relevant here. Your code makes a table with two columns and x rows but I want the table header to be only on one column. Should I do a merge of the first row at the end of the function?Maxbester

1 Answers

0
votes

The best thing you can do to accomplish what you are after is setting up the dimensions of the columns at the start, before performing any modification. Sample code:

Dim availableWidth As Integer
availableWidth = wrdDoc.PageSetup.PageWidth - wrdDoc.PageSetup.LeftMargin - wrdDoc.PageSetup.RightMargin

With wrdTppTable
   .Columns.Add 'Adding the required column
    'Resizing both columns on account of the available space
   .Columns(1).Width = availableWidth / 2
   .Columns(2).Width = availableWidth / 2
   .Cell(1, 1).Merge .Cell(1, 2)
End With

Right after this code you can start iterating through the cells and performing the actions you want, just by adding rows. Use Cells.Split only in case of really needing it; for example: in the third row you want to have three columns, two of them fitting within the main second column.