使用VBA在MS Word中创建表

Doo*_*106 3 vba ms-word word-vba

在MS Word中创建表的代码是什么?

我已经使用宏的记录功能来查看它是如何编写的,我不知道如何解释它.

你能不能在录制宏时画桌子?它使"绘制表"功能变灰.

我需要制作一个在表格中有一些合并单元格的talbe - 如果我可以绘制表格并使用宏记录会更容易但似乎我不能那样做...

表

我已经使用绘图功能绘制了下表,但我无法记录它.

救命?!

Tim*_*ams 8

这应该让你开始.

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
Run Code Online (Sandbox Code Playgroud)