4
votes

What are the codes to create a table in MS Word?

I've used the record function for macro to see how it is written and i have no idea how to interpret this.

and can you not draw tables while recording a macro? It greys out the "draw table" function.

I need to make a talbe that has some merged cells within the table - it would be easier if i can draw the table and record using macro but it seems like i can't do that...

table

I've used the draw function to draw the follow table but i can't record it.

HELP?!

3
Why not just copy that table from a template document?Tim Williams
what do you mean template document? the thing is, i want to creat the exact same table everytime i click the macro. because i need to create the same table multiple timesDoolie1106
I mean just create that table in a word doc, then in your macro load up the doc and copy the table to wherever you need it.Tim Williams
but everytime i run the macro, it doesn't paste the table but rather it pastes the page? i'm not sure if that makes sense, when i try to copy the table while recording a macro, i can't drag and highlight to copy the table. so I did alt+A to copy the table and pasted it. but everytime i run the macro it would paste the page when i only want to paste the table?Doolie1106

3 Answers

9
votes

This should get you started.

Sub Tester()


    Dim x, w, c

    ThisDocument.Tables(1).Delete

    ThisDocument.Tables.Add Range:=Selection.Range, NumRows:=7, NumColumns:=1, _
                          DefaultTableBehavior:=wdWord9TableBehavior, _
                          AutoFitBehavior:=wdAutoFitFixed

    With ThisDocument.Tables(1)

        .Rows.Height = 70
        w = .Rows(1).Cells(1).Width

        .Rows(1).Cells(1).Split 1, 7
        .Rows(1).Cells(1).Width = w / 2
        For x = 2 To 7
            .Rows(1).Cells(x).Width = (w / 2) / 6
        Next x

        .Rows(5).Height = 15
        .Rows(7).Height = 15

        .Rows(7).Cells(1).Split 1, 7

        .Rows(6).Cells(1).Split 1, 4
        .Rows(6).Cells(2).Split 2, 1

        'Once you merge cells it gets difficult to use .Rows, but
        '  you can still address individual cells. Use the loop below to
        '  find out which one you need to operate on...
        x = 1
        For Each c In .Range.Cells
            c.Range.Text = x
            x = x + 1
        Next c

        .Range.Cells(16).Split 1, 4
        'you can figure out setting the exact required widths...
    End With
End Sub
4
votes

the basic command for making a table is

ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=4, NumColumns:= 3

and to split/merge cells:

Selection.Cells.Split NumRows:=1, NumColumns:=2
Selection.Cells.Merge
0
votes

You could create the table and then save it as an autotext (select table - ALT+F3 - name of the autotext). Then when you need the table in a document, just type the name you gave it and press F3.