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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5885299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:09:49  来源:igfitidea点击:

how to create a table in a word 2003 document using VBA

vbams-wordword-2003

提问by mark

I have a template for a report that needs to get filled out and i am automating the process.

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

There is a part of the template a couple pages down that has multiple identical tables to input data.

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

What I am trying to do is have a user control with a text box where the user can input a number and then the document generates the number of tables specified.

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

I am not sure where to start and how to specify where the tables are to be generated in relation to the rest of the document,

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

回答by ray

Created the base code via the macro recorder and then added the vars and loop:

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

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