繁体   English   中英

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

[英]how to create a table in a word 2003 document using VBA

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

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

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

我不确定从哪里开始以及如何指定与文档的 rest 相关的表格的生成位置,

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM