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

发布于 2024-11-05 11:42:23 字数 155 浏览 7 评论 0原文

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

模板的一部分有多个相同的表来输入数据。

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

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

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,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失与倦" 2024-11-12 11:42:23

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

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文