如何使用VBA在Word 2003文档中创建表

时间:2011-05-04 14:47:07

标签: vba ms-word word-2003

我有一个需要填写的报告模板,我正在自动化该过程。

模板的一部分有几页向下,有多个相同的表来输入数据。

我要做的是拥有一个带有文本框的用户控件,用户可以在其中输入数字,然后文档生成指定的表数。

我不知道从哪里开始以及如何指定相对于文档其余部分生成表的位置,

1 个答案:

答案 0 :(得分:3)

通过宏录制器创建基本代码,然后添加变量和循环:

Sub tableMake()

    Dim numberOfTables As Integer
    Dim iCount As Integer

    numberOfTables = InputBox("How many tables to make?", "Tables")

    For iCount = 0 To numberOfTables - 1

        ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2, NumColumns:= _
            3, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
            wdAutoFitFixed
        With Selection.Tables(1)
            If .Style <> "Table Grid" Then
                .Style = "Table Grid"
            End If
            .ApplyStyleHeadingRows = True
            .ApplyStyleLastRow = False
            .ApplyStyleFirstColumn = True
            .ApplyStyleLastColumn = False
            '.ApplyStyleRowBands = True 'Office 2010
            '.ApplyStyleColumnBands = False 'Office 2007
        End With

        Selection.EndKey Unit:=wdStory
        Selection.TypeParagraph

    Next iCount

End Sub
相关问题