简体   繁体   中英

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

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,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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