4

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

我已经使用宏的记录函数来查看它是如何编写的,但我不知道如何解释它。

录制宏时不能画表吗?它使“绘图表”功能变灰。

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

桌子

我已经使用draw函数绘制了下表,但我无法记录它。

帮助?!

4

3 回答 3

9

这应该让你开始。

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
于 2012-11-13T20:23:22.367 回答
4

制作表格的基本命令是

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

并拆分/合并单元格:

Selection.Cells.Split NumRows:=1, NumColumns:=2
Selection.Cells.Merge
于 2012-11-13T19:11:26.660 回答
0

您可以创建表格,然后将其保存为自动文本(选择表格 - ALT+F3 - 自动文本的名称)。然后,当您需要文档中的表格时,只需键入您给它的名称并按 F3。

于 2016-03-28T19:09:38.140 回答