creating tables in MS Word using VBA

我怕爱的太早我们不能终老 提交于 2019-12-10 10:05:47

问题


What are the codes to create a table in MS Word?

I've used the record function for macro to see how it is written and i have no idea how to interpret this.

and can you not draw tables while recording a macro? It greys out the "draw table" function.

I need to make a talbe that has some merged cells within the table - it would be easier if i can draw the table and record using macro but it seems like i can't do that...

I've used the draw function to draw the follow table but i can't record it.

HELP?!


回答1:


This should get you started.

Sub Tester()


    Dim x, w, c

    ThisDocument.Tables(1).Delete

    ThisDocument.Tables.Add Range:=Selection.Range, NumRows:=7, NumColumns:=1, _
                          DefaultTableBehavior:=wdWord9TableBehavior, _
                          AutoFitBehavior:=wdAutoFitFixed

    With ThisDocument.Tables(1)

        .Rows.Height = 70
        w = .Rows(1).Cells(1).Width

        .Rows(1).Cells(1).Split 1, 7
        .Rows(1).Cells(1).Width = w / 2
        For x = 2 To 7
            .Rows(1).Cells(x).Width = (w / 2) / 6
        Next x

        .Rows(5).Height = 15
        .Rows(7).Height = 15

        .Rows(7).Cells(1).Split 1, 7

        .Rows(6).Cells(1).Split 1, 4
        .Rows(6).Cells(2).Split 2, 1

        'Once you merge cells it gets difficult to use .Rows, but
        '  you can still address individual cells. Use the loop below to
        '  find out which one you need to operate on...
        x = 1
        For Each c In .Range.Cells
            c.Range.Text = x
            x = x + 1
        Next c

        .Range.Cells(16).Split 1, 4
        'you can figure out setting the exact required widths...
    End With
End Sub



回答2:


the basic command for making a table is

ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=4, NumColumns:= 3

and to split/merge cells:

Selection.Cells.Split NumRows:=1, NumColumns:=2
Selection.Cells.Merge



回答3:


You could create the table and then save it as an autotext (select table - ALT+F3 - name of the autotext). Then when you need the table in a document, just type the name you gave it and press F3.



来源:https://stackoverflow.com/questions/13366136/creating-tables-in-ms-word-using-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!